Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.35/5 (6 votes)
See more:
How to create a text file in C# ?
Posted

To write text to file
System.IO.File.WriteAllText
method and to append text to the existing file
System.IO.File.AppendAllText
method can be used

An example is given here
http://msdn.microsoft.com/en-us/library/ms143375.aspx[^]
This may be helpful to you.
 
Share this answer
 
I'm sorry Google is broken. File.Create[^]
 
Share this answer
 
If you need only use file class, this is the solution for you,

C#
File.WriteAllText("D:\\as.txt", "welcome");
 
Share this answer
 
Comments
P.Kamaraju 28-Mar-12 9:16am    
Thank you.
You can use this method to just to create a text file.

C#
File.Create("D:\\Yourfile.txt");


for that you need to use this namespace

C#
using System.IO;


If you need to read and write to a file use below code. It will do the work.


C#
//this code segment write data to file.
FileStream fs1 = new FileStream("D:\\Yourfile.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs1);
writer.Write("Hello Welcome");
writer.Close();

//this code segment read data from the file.
FileStream fs2 = new FileStream("D:\\Yourfile.txt", FileMode.OpenOrCreate, FileAccess.Read);
StreamReader reader = new StreamReader(fs2);
textBox1.Text = reader.ReadToEnd();
reader.Close();
 
Share this answer
 
v3
Comments
neymar1107 7-Jan-14 5:12am    
Thanks
C#
string path = @"C:\mytesttext.txt";
string text2write = "Hello World!";

System.IO.StreamWriter writer = new System.IO.StreamWriter(path);
writer.Write(text2write);
writer.Close();


Can be found anywhere on the net. What did you do yourself? Did you even open a book or do some google?

Please before copy/pasting this code, go to MSDN and learn about the StreamWriter class.

hope this helps.
 
Share this answer
 
Comments
P.Kamaraju 28-Mar-12 8:56am    
Hello....

I have gone through the respective documents(MSDN,Google,...). but i was trying get this solution by using File Class.
like File.Create() or File.delete()

How can i write some text in a file without using any Stream classes ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900