Its me
Enthusiast
C# Read / Write Text Files Tutorial
Requirements
Visual Studio C# Express Edition 2008 - 2010
---------------------------
Step 1: Creating Project
---------------------------
Create a new project named "ReadWriteTextFile"
------------------------
Step 2: Adding Controls
------------------------
Adding Controls
-------------------
Step 3: Preparing
-------------------
Double Click Your Form .
And at the very top add this code along with the lines of code similar to it.
This will allow us to use the StreamReader and StreamWriter Methods needed to Read / Write our text file.
----------------------------
Step 4: Coding Save Button
----------------------------
Go Back To The Design Tab.
Double Click "SaveButton".
Add the following code.
Code:
--------------------------
Step 5: Coding Load Button
--------------------------
Go To The Design Window And Double Click "LoadButton"
Add The Following Code
--------------------------------
Step 6 : End Of Tutorial.
--------------------------------
Requirements
Visual Studio C# Express Edition 2008 - 2010
---------------------------
Step 1: Creating Project
---------------------------
Create a new project named "ReadWriteTextFile"
------------------------
Step 2: Adding Controls
------------------------
Adding Controls
Add a richtextbox and change its Name property.
Name: "RichTextBox"
Add a Button and change its name and text properties.
Name: "SaveButton"
Text: "Save"
Add another Button and change its name and text properties.
Name: "LoadButton"
Text: "Load"
Name: "RichTextBox"
Add a Button and change its name and text properties.
Name: "SaveButton"
Text: "Save"
Add another Button and change its name and text properties.
Name: "LoadButton"
Text: "Load"
-------------------
Step 3: Preparing
-------------------
Double Click Your Form .
And at the very top add this code along with the lines of code similar to it.
Code:
using System.IO;
----------------------------
Step 4: Coding Save Button
----------------------------
Go Back To The Design Tab.
Double Click "SaveButton".
Add the following code.
Code:
Code:
//Create A new string to hold the Selected File Path.
string FilePath;
//Create A new Save File Dialog To Select Desired Save Location.
SaveFileDialog Sfd = new SaveFileDialog();
Sfd.Filter = "Rich Text Format (*.rtf)|*.rtf|Text Files (*.txt)|*.txt";
//Show The Sfd SaveFile Window.
//If Sfd.ShowDialog Does Not = DialogResult.OK then Stop Saving
if (Sfd.ShowDialog() != DialogResult.OK)
{
return;
}
//string FilePath = The Selected Save Location
FilePath = Sfd.FileName;
//Save Text File
//Initialize a new instance of StreamWriter to Write Files
//StreamWriter SaveFile = new StreamWriter(Save Location);
StreamWriter SaveFile = new StreamWriter(FilePath);
SaveFile.Write(RichTextBox.Text);
SaveFile.Close();
--------------------------
Step 5: Coding Load Button
--------------------------
Go To The Design Window And Double Click "LoadButton"
Add The Following Code
Code:
//Create A new string to hold the Selected File Path.
string FilePath;
//Create A new Open File Dialog To Select Desired File To Laod.
OpenFileDialog Ofd = new OpenFileDialog();
Ofd.Filter = "Rich Text Format (*.rtf)|*.rtf|Text Files (*.txt)|*.txt";
//Show The Ofd Open File Window.
//If Ofd.ShowDialog Does Not = DialogResult.OK then Stop Loading
if (Ofd.ShowDialog() != DialogResult.OK)
{
return;
}
//string FilePath = The Selected File To Load Location
FilePath = Ofd.FileName;
//Load File Text Into RichTextBox
//Initialize a new instance of StreamReader to Read Files
//StreamReader LoadFile = new StreamReader(Load File Location);
StreamReader LoadFile = new StreamReader(FilePath);
string LoadedFileText = LoadFile.ReadToEnd();
RichTextBox.Text = LoadedFileText;
--------------------------------
Step 6 : End Of Tutorial.
--------------------------------