Skip to content

Using Stream Reader and Stream Writer (C#)

June 28, 2010

Example using Stream Reader

string File = Server.MapPath("/test/test.html");
StreamReader sr = new StreamReader(File);
string sreader = sr.ReadToEnd();
sr.Close();

Explanation :
It used to read string until the end of file test.html in a virtual directory “/test/test.html”

Example using Stream Writer

string directory = "C:\\Documents and Settings\\riani\\Desktop\\TESPDF\\TESPDF\\TESPDF\\test";
StreamWriter sWriter = File.CreateText(directory + "\\name.html");
sWriter.Write(sreader);
sWriter.Close();

Explanation :
It used to write string, which result of stream reader before, to a new created file at the certain directory and a file named as name.html.

Notice that after finished using both streams, don’t forget to close it.

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.