Search Forum
(57415 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


              
Printable Version

How to Open and Read a File in Win Forms
By Baseer Ahmed

In this tutorial I am going to tell you about how to open your file and display it in the text box and how to save your 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 text file 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 appear below.

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 give instructions to the user that what to do with the text box. Now double click the button "open text file to read" and then add the library "using System.IO" .After this make a following function in the class as:

private string Read( string file )
{

 StreamReader reader = new StreamReader( file );
 string data = reader.ReadToEnd ();
 reader.Close();

 return data;
}
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 string of characters).in the braces we write the code of reading from the text file .here StreamReader is associated with System.IO class ,we have created object of StreamReader and reserved space for the file with new keyword ,then we read that file from start to end and stored it in the data variable of type string, and return that data. After adding this code we will ad the following code in the button function..

textBox1.Text = "";           
DialogResult result = openFileDialog1.ShowDialog();
if ( result == DialogResult.OK )
{
 string data = Read( openFileDialog1.FileName );
 textBox1.Text = data;
}
else
{

  //do nothing
}
In the above code we have just cleared the textbox by "textBox1.Text = """.then in the next statement we opened the dialog box "DialogResult result= openFileDialog1.ShowDialog()" .In the next statement we used if else statement to ensure if the user has selected the file then immediately the Read() function will be called(which we have created above) and it will read from the file and store that contents of the file in the variable of type "string", and display it in the text box and if user doesn't select the text file then do nothing.

HOW TO SAVE FILE

Now I am going to tell you about how to save your file where ever you want in windows forms . Firstly go to the tool bar and double click on option named "SaveFileDialog"(same procedure as above).Then Create a button and in properties set its text to "Save your file(write extension also)" and its name to "btn2".Double click on this 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 function save and passed two parameters in it of type string ,next in the braces we 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 save button then the save dialog will appear where file name is entered normally in the dialog box and stored in the variable "file" of type string and contents of the text box which is entered by the user is stored in variable of type "data".
Then on clicking save button in dialog Box the file will be saved..
Author: BASEER AHMED
Student of: (Bachelor of science in telecommunication engineering)
National university of computer and emerging sciences (FAST)
Pakistan.
E-mail: baseerhmd@gmail.com