c#中如何向文本文件中写入字符串。最好写个小程序示范一下。谢谢

2024-12-14 01:29:50
推荐回答(2个)
回答1:

try
{

//Pass the filepath and filename to the StreamWriter Constructor
StreamWriter sw = new StreamWriter("C:\\Test.txt");

//型茄游Write a line of text
sw.WriteLine("Hello World!!");

//Write a second line of text
sw.WriteLine("From the StreamWriter class");

//Close the file
sw.Close();
}
catch(Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block."卜销纳缺);
}

回答2:

StreamWriter sw=new StreamWriter("D:\让扰\test.txt");
sw.AppendLine("坦码旦this is line!"模档);
sw.Close();