C# StreamWriter.Write 不写入文件;没有异常抛出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11023868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
StreamWriter.Write doesn't write to file; no exception thrown
提问by sarsnake
My code in C# (asp.net MVC)
我的 C# 代码(asp.net MVC)
StreamWriter tw = new StreamWriter("C:\mycode\myapp\logs\log.txt");
// write a line of text to the file
tw.Write("test");
The file is created but is empty. No exception is thrown. I have never seen this before and I am stuck here; I just need to write some debugging output.
该文件已创建但为空。不会抛出任何异常。我以前从未见过这种情况,我被困在这里;我只需要写一些调试输出。
Please advise.
请指教。
采纳答案by crassr3cords
Use
用
System.IO.File.WriteAllText(@"path\te.txt", "text");
回答by Benjamin Cox
StreamWriter is buffered by default, meaning it won't output until it receives a Flush() or Close() call.
StreamWriter 默认是缓冲的,这意味着它在收到 Flush() 或 Close() 调用之前不会输出。
You can change that by setting the AutoFlushproperty, if you want to. Otherwise, just do:
如果需要,您可以通过设置AutoFlush属性来更改它。否则,只需执行以下操作:
StreamWriter tw = new StreamWriter("C:\mycode\myapp\logs\log.txt");
// write a line of text to the file
tw.Write("test");
tw.Close(); //or tw.Flush();
回答by Tony Hopkinson
Neither flushed nor closed nor disposed. try this
既不冲洗也不关闭也不处置。尝试这个
using (StreamWriter tw = new StreamWriter(@"C:\mycode\myapp\logs\log.txt"))
{
// write a line of text to the file
tw.Write("test");
tw.Flush();
}
or my preference
或者我的偏好
using (FileStream fs = new FileStream( @"C:\mycode\myapp\logs\log.txt"
, FileMode.OpenOrCreate
, FileAccess.ReadWrite) )
{
StreamWriter tw = new StreamWriter(fs);
tw.Write("test");
tw.Flush();
}
回答by user1415567
an alternative
替代
FileStream mystream = new FileStream("C:\mycode\myapp\logs\log.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter tw = new StreamWriter(mystream);
tw.WriteLine("test");
tw.close();
回答by Hai Bi
Try to close the file or add \nto the line such as
尝试关闭文件或添加\n到诸如
tw.WriteLine("test");
tw.Close();
回答by shivesh suman
Ya in VB.net this was not needed but it seems with CSharp you need a Writer.Flush call to force the write. Of course Writer.Close() would force the flush as well.
在 VB.net 中,这不是必需的,但似乎在 CSharp 中您需要 Writer.Flush 调用来强制写入。当然 Writer.Close() 也会强制刷新。
We can also set the AutoFlush Property of the StreamWriter instance:
我们还可以设置 StreamWriter 实例的 AutoFlush 属性:
sw.AutoFlush = true;
// Gets or sets a value indicating whether the StreamWriter
// will flush its buffer to the underlying stream after every
// call to StreamWriter.Write.
From: http://msdn.microsoft.com/en-us/library/system.io.streamwriter.autoflush(v=vs.110).aspx
来自:http: //msdn.microsoft.com/en-us/library/system.io.streamwriter.autoflush(v=vs.110).aspx
回答by pedram bashiri
You need to either close or flush the StreamWriter after finishing writing.
完成写入后,您需要关闭或刷新 StreamWriter。
tw.Close();
or
或者
tw.Flush();
But the best practice is to wrap the output code in a using statement, since StreamWriter implements IDisposable:
但最佳实践是将输出代码包装在 using 语句中,因为 StreamWriter 实现了 IDisposable:
using (StreamWriter tw = new StreamWriter("C:\mycode\myapp\logs\log.txt")){
// write a line of text to the file
tw.Write("test");
}
回答by Phani Tekkem
FileStream fs = new FileStream("d:\demo.txt", FileMode.CreateNew,
FileAccess.Write);
StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.ASCII);
int data;
sw.Write("HelloWorld");
sw.Close();
fs.Close();
- The problem is when StreamWriter Object is created with reference of FileStream Object , SW object will be always expecting some data till SW object is Closed.
- So After using sw.Close();Your Opened File will get closed and get ready for showing Output.
- 问题是当 StreamWriter 对象是通过引用 FileStream Object 创建时,SW 对象将始终期待一些数据,直到 SW 对象关闭。
- 所以在使用sw.Close(); 之后 您打开的文件将被关闭并准备好显示输出。

