First of all StreamWriters and StreamReaders are used for writing or reading from some .txt file, later you can do whatever you want from them, put them into loops, or whatever. So let's start.
First thing you will do is to call IO(input/output) library.
using System.IO;Than you can put start using streamwriter or reader in events. In this case I will be using an openFileDialog and saveFileDialog to show you how you can select file you want to read or give the file a path where you want to save it:
private void button1_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
sw.Write(richTextBox1.Text);
sw.Close();
}
}
Now I will explain every lineFirst of all you see that if you want to save something to a .txt file you must use savefiledialog, and streamWriter (it's not necessary to use save file dialog, you can just put a path whre you want to save it)
So first of all you create new streamwriter by telling him where to save that file( in this case this is the path you select from save file dialog).
Second you use Write() method, to write all text from textbox, you can also use WriteLine() method for writing just a single line.
If you call this:
sw.WriteLine(richtextbox1.Text); sw.WriteLine(richtextbox1.Text);you will get just first 2 lines from that text box.
Okay, so you finished writing to a .txt file, now what you are going to do is to close that writer by calling a method Close();
You need to close reader or writer every time when you finish because if you dont, he will just continue and you will get an error.
In this example I will show just some examples of path usage in streamwriter(the same is used for readers).
StreamWriter sw = new StreamWriter(@"C:\\Documents\YourFolder", "filename.txt"); //example of giving exact path where you want to save something, you can put whatever path you want I'm just giving an example.
StreamWriter sw = new StreamWriter("filename.txt"); //It puts your file to the Debug Folder, right next to the .exe file
StreamWriter sw = new StreamWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop, "filename.txt"); // It puts file to the desktop, on every computer.
Okay now that you know what StreamWriter is, let's go to StreamReader, it is pretty much the same.In this example I will show how to read to textbox from txt file, calling a streamreader from openFileDialog.
private void button2_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new StreamReader(openFileDialog1.FileName);
richTextBox1.Text = sr.ReadToEnd();
sr.Close();
}
}
So basically it is the same, First line of StreamReader is the same as StreamWriter, just for reader you put path where you want it to READ from.Now i will explain just 2 of the most used methods.
ReadToEnd() - reads all text from .txt file and puts it into a textbox or label or whatever you put.
ReadLine() - reads just one line.
You can also set streamreader an exact path: @"location". So everything I told for StreamWriter stays for StreamReader.
I hope I helped, and that you now understand writer and reader.
Tutorial by Cushpajz.
Ps. If you are putting this in other forums, please put an author. Thanks.


Sign In
Create Account


Back to top










