Friday 10 October 2014

What is FileStream Class in C# and How to applied read/write Operation with its

What is FileStream and File Class in C# and How to applied read/write Operation with its......

FileStream can be used to perform the basic operations of reading and writing operating system files. To use this class, you must first declare a variable of the class FileStream.

Parameters:

  • Path:  A relative or absolute path for the file that the current FileStream object will encapsulate.
  • Mode: A constant that determines how to open or create the file.
  • Access: A constant that determines how the file can be accessed by the FileStreamobject. This gets the System.IO.FileStream.CanRead ,System.IO.FileStream.CanWrite properties of the FileStream object. System.IO.FileStream.CanSeek is true if path specifies a disk file.
Exceptions:

  1. System.ArgumentNullException:nd 
  2. System.ArgumentException:
  3. System.NotSupportedException:
  4. System.IO.FileNotFoundException:
  5. System.IO.IOException:
  6. System.Security.SecurityException:
  7. System.IO.PathTooLongException:
  8. System.ArgumentOutOfRangeException:
  9. System.UnauthorizedAccessException:
  10. System.IO.DirectoryNotFoundException:
  11. System.UnauthorizedAccessException:

Source Code:…………….

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
// using this  namespace
using System.IO;

namespace BasicwithFileHanding
{
public partial class FileStreamConcept : Form
{
public FileStreamConcept()
{
InitializeComponent();
}


string filepath, strcontent;

// code for how to create TextBox with Browse Concept with using openFileDialog1……………………

private void btnBrowse_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtselectedFile.Text = openFileDialog1.FileName;
filepath = txtselectedFile.Text;
}
}

//  create method for WriteFile………………………..

public void WriteFile(string FileName, string content)
{
FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write);
if (fs.CanWrite)
{
byte[] buffer = Encoding.ASCII.GetBytes(content);
fs.Write(buffer, 0, buffer.Length);
}
fs.Flush();
fs.Close();

}
string result;

//  create method for ReadFile…………………

public void ReadFile(string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
if (fs.CanRead)
{
byte[] buffer = new byte[fs.Length];
int bytesread = fs.Read(buffer, 0, buffer.Length);
result = Encoding.ASCII.GetString(buffer, 0, bytesread);
}

fs.Close();
txtesult.Text = result;
}
// Write Button Click…………………

private void btnWrite_Click(object sender, EventArgs e)
{
strcontent = txtesult.Text;
WriteFile(filepath, strcontent);
MessageBox.Show("Write Successfully");

}
//Read Button Click…………………

private void btnRead_Click(object sender, EventArgs e)
{
ReadFile(filepath);
}
}
}

//Result…………………




Note : You can also do above  Concept with File.WriteAllText and File.ReadAllText  and we  will  also  use  openFileDialog1 and saveFileDialog1 in this  code



public static string[] ReadAllLines(string path, Encoding encoding);

Summary: Opens a text file, reads all lines of the file, and then closes the file.
Parameters:
Path: The file to open for reading.
Returns: A string containing all lines of the file.

public static void WriteAllLines(string path, string[] contents, Encoding encoding);

   
Summary: Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.
 Parameters:
        path: The file to write to.
        contents:The string to write to the file.

Source Code:…………….
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
// using this name  space
using System.IO;

namespace BasicwithFileHanding
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


string filepath;
// code for how to create TextBox with Browse Concept with using openFileDialog1
private void btnBrowse_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
txtselectedFile.Text = openFileDialog1.FileName;
filepath =txtselectedFile.Text;
}
}
//code for Write
private void btnWrite_Click(object sender, EventArgs e)
{

if(saveFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
File.WriteAllText(filepath, txtesult.Text);
}
//code for Read
private void btnRead_Click(object sender, EventArgs e)
{
filepath =txtselectedFile.Text;
string result= File.ReadAllText(filepath);
txtesult.Text = result;

}
}
}

Result




0 comments:

Post a Comment