What's new

C# Tutorial(Read/Write Text File)

Zaid

Its me

Enthusiast
Messages
336
Reaction score
47
Points
85
Sin$
0
C# Read / Write Text Files Tutorial
Requirements
Visual Studio C# Express Edition 2008 - 2010


---------------------------
Step 1: Creating Project
---------------------------
Create a new project named "ReadWriteTextFile"

6dvmn5.png

------------------------
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"
125ruxu.png


-------------------
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;
This will allow us to use the StreamReader and StreamWriter Methods needed to Read / Write our text file.

27x48qu.png

----------------------------
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.
--------------------------------
 
kiwimoosical

kiwimoosical

Getting There
Messages
1,123
Reaction score
474
Points
205
Sin$
7
Code:
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Text Files (*.txt)|*.txt";
if(ofd.ShowDialog() == DialogResult.OK)
File.WriteAllText(ofd.FileName, richTextBox1.Text);
Shorter. :biggrin:
 
Zaid

Its me

Enthusiast
Messages
336
Reaction score
47
Points
85
Sin$
0
Code:
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Text Files (*.txt)|*.txt";
if(ofd.ShowDialog() == DialogResult.OK)
File.WriteAllText(ofd.FileName, richTextBox1.Text);
Shorter. :biggrin:
That wasn't why I was writing this tutorial though.
The entire point of the tutorial was how to use Streamreader / Writer.
 
Bird is the word

PH2

Music don't lie.
Messages
642
Reaction score
77
Points
105
Sin$
0
I've only just looked at this, still thnx, nice thread a lot of help
 
GoldBl4d3

GoldBl4d3

VIP
VIP
Retired
Programmer Mythical Veteran Legendary Veteran
Messages
3,785
Reaction score
1,452
Points
600
Sin$
0
Nice, good for begginners. Hate to say though, the code is slightly ineffeicient for memory usage. First being that you are storing the filepath in a string. When in this example, their is no point in doing that. It will be useful if its need again, but in this case not. Other thing, I see you check to see if the dialog result isnt 'OK' and if so it returns to the parent method. Its a great technique if you are performing checks and balances, but in this example, it is just more code that actually required. Lastly, your comments need to be more indepth to teach someone. Giving the code and writing a comment of 'WHAT' your doing with it teaches no one. Instead, write a comment for each line explaining what it is, why your using it, and what it can be used for.

Overall, I think its great for learners to be introduced in to IO. (Maybe oneday people will start teaching learners different stuff than IO and modding programs).
 
Top Bottom
Login
Register