How to Open and Read a File in Win Forms using C#
In this tutorial I am going to tell you abouthow to open your file and display it in the text box and how to saveyour file anywhere .HOW TO OPEN (READ) A FILE
Open your new project and name it "filestream",then create a button and in properties change its text to "open textfile to read" and its name to "btn1".Then double click on option in the toolbar named "open file dialog",immediately the in the form design the "OpenFileDialog1" will appearbelow.
Also Create simple text box and in properties change its text to blank and its name to "text box1".
Also add label to text box where you can giveinstructions to the user that what to do with the text box.Now double click the button "open text file to read" and then add thelibrary "using System.IO" .After this make a following function in theclass as:
private string Read( string file )
{
{
StreamReader reader = new StreamReader( file );
string data = reader.ReadToEnd ();
reader.Close();
return data;string data = reader.ReadToEnd ();
reader.Close();
}
The explanation of this code is very simple.here we have just created a Read function and give it the parameter"file" of type string (so that whatever in the file is read as a stringof characters).in the braces we write the code of reading from the textfile .here StreamReader is associated with System.IO class ,we havecreated object of StreamReader and reserved space for the file with newkeyword ,then we read that file from start to end and stored it in thedata variable of type string, and return that data.After adding this code we will ad the following code in the buttonfunction..
textBox1.Text = "";
DialogResult result = openFileDialog1.ShowDialog();
if ( result == DialogResult.OK )
{
string data = Read( openFileDialog1.FileName );
textBox1.Text = data;
}
else
{
//do nothingDialogResult result = openFileDialog1.ShowDialog();
if ( result == DialogResult.OK )
{
string data = Read( openFileDialog1.FileName );
textBox1.Text = data;
}
else
{
}
In the above code we have justcleared the textbox by "textBox1.Text = """.then in the next statementwe opened the dialog box "DialogResult result=openFileDialog1.ShowDialog()" .In the next statement we used if elsestatement to ensure if the user has selected the file then immediatelythe Read() function will be called(which we have created above) and itwill read from the file and store that contents of the file in thevariable of type "string", and display it in the text box and if userdoesn't select the text file then do nothing.
HOW TO SAVE FILE
Now I am going to tell you about how to saveyour file where ever you want in windows forms . Firstly go to the toolbar and double click on option named "SaveFileDialog"(same procedure asabove).Then Create a button and in properties set its text to "Saveyour file(write extension also)" and its name to "btn2".Double click onthis button and write following function in the class..private void Save( string file, string data )
{
StreamWriter writer = new StreamWriter(file);
writer.Write(data);
writer.Close();
}
in the above code we have created the functionsave and passed two parameters in it of type string ,next in the braceswe simply write code for writing in the file .here Stream Writer(similar to StreamReader) is associated with the library System.IO.
After this add the following code in the button function..
DialogResult result = saveFileDialog1.ShowDialog();
string file=saveFileDialog1.FileName.ToString();string data = textBox1.Text;
Save( file, data );
In the above code when user click on savebutton then the save dialog will appear where file name is enterednormally in the dialog box and stored in the variable "file" of typestring and contents of the tex
No comments:
Post a Comment