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.
请指教。
8 个解决方案
#1
6
Use
使用
System.IO.File.WriteAllText(@"path\te.txt", "text");
#2
40
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 AutoFlush property, 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();
#3
5
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();
}
#4
3
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");
}
#5
2
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.
- 问题是当StreamWriter对象是在引用FileStream对象的情况下创建的,SW对象总是会期待一些数据直到SW对象被关闭。
- So After using sw.Close(); Your Opened File will get closed and get ready for showing Output.
- 所以在使用sw.Close()之后;您的已打开文件将关闭并准备好显示输出。
#6
1
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
#7
0
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();
#8
0
Try to close the file or add \n
to the line such as
尝试关闭文件或将\ n添加到行等
tw.WriteLine("test");
tw.Close();
#1
6
Use
使用
System.IO.File.WriteAllText(@"path\te.txt", "text");
#2
40
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 AutoFlush property, 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();
#3
5
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();
}
#4
3
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");
}
#5
2
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.
- 问题是当StreamWriter对象是在引用FileStream对象的情况下创建的,SW对象总是会期待一些数据直到SW对象被关闭。
- So After using sw.Close(); Your Opened File will get closed and get ready for showing Output.
- 所以在使用sw.Close()之后;您的已打开文件将关闭并准备好显示输出。
#6
1
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
#7
0
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();
#8
0
Try to close the file or add \n
to the line such as
尝试关闭文件或将\ n添加到行等
tw.WriteLine("test");
tw.Close();