Tuesday 30 June 2015

How to Create or Overwrite Text file in app.net using C#



Create a Text File in C#

Description:-

In this example we explain that how to create text file in asp.net or write data to the text file fetching from the database.

Here generally we create new file and write data to text file from the database.below is code for creating text file and write data to it.




fs1 = new FileStream(Server.MapPath("Files\\demo.txt"), FileMode.OpenOrCreate, FileAccess.Write);

writer.Write("Name :" + Convert.ToString(dtDetails.Rows[0]["name"]) + Environment.NewLine);
                    writer.Write("City :" + Convert.ToString(dtDetails.Rows[0]["Clty"]) + Environment.NewLine);
                    writer.Write("Country" + Convert.ToString(dtDetails.Rows[0]["Country"]) + Environment.NewLine);
writer.Close();

you can also overwrite the existing file if exist like just write
fs1 = new FileStream(Server.MapPath("Files\\demo.txt"), FileMode.Create);



0 comments:

Post a Comment