[英]Why StreamWriter can't write text into file in c#?
I'm new in c#, and I'm writing a some text into a file, for this purpose, I'm using a source code which I found searching on Google:我是 c# 新手,正在将一些文本写入文件,为此,我使用了在 Google 上搜索的源代码:
FileStream fs = System.IO.File.OpenWrite(Server.MapPath("~/FILE/") + logFile);
StreamWriter sw = new StreamWriter(fs);
//sw.Write(DateTime.Now.ToString() + " sent email to " + email);
sw.Write(" sent email to " );
fs.Close();
This code runs, but when I open text file, I can't see any data in it, what is happening?这段代码运行了,但是当我打开文本文件时,我看不到其中的任何数据,这是怎么回事? How can I solve this problem?
我怎么解决这个问题?
Modified your code as below.修改你的代码如下。 hope you are looking for this kind.
希望你正在寻找这种。
using (FileStream fs = System.IO.File.OpenWrite(Server.MapPath("~/FILE/") + logFile))
{
using (StreamWriter sw = new StreamWriter(fs))
{
//sw.Write(DateTime.Now.ToString() + " sent email to " + email);
sw.Write(" sent email to ");
}
fs.Close();
}
Try simple File.AppendAllText
尝试简单的
File.AppendAllText
File.AppendAllText(Path.Combine(Server.MapPath("~/FILE/"), logFile),
string.Format("{0} sent email to {1}", DateTime.Now, email));
to append the log file instead using streams and writers.使用流和编写器来附加日志文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.