C# StreamWriter - 不附加到创建的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1154119/
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 - Not appending to created file
提问by
I am using the StreamWriter object to write to either a file that is created by the constructor or already exists. If the file exists then it appends data. If not, then it should create a file and then also append data. The problem is that when the file needs to be created, the StreamWriter constructor creates the file but does not write any data to the file.
我正在使用 StreamWriter 对象写入由构造函数创建或已存在的文件。如果文件存在,则它附加数据。如果没有,那么它应该创建一个文件,然后附加数据。问题在于,当需要创建文件时,StreamWriter 构造函数会创建文件,但不会向文件写入任何数据。
bool fileExists = File.Exists(filePath);
using (StreamWriter writer = new StreamWriter(filePath, true))
{
if (!fileExists)
{
writer.WriteLine("start");
}
writer.WriteLine("data");
}
EDIT: Thanks for the answers. The using block takes care of closing the writer. As for other people saying it works for them, is there any information I can give you to further diagnose the problem? The file is localed across a network. Could that be a potential problem. Intermittently I receive the errors, "Could not find a part of the path ..." and "The specified network name is no longer available."
编辑:感谢您的回答。using 块负责关闭编写器。至于其他人说它对他们有用,我可以给你任何信息来进一步诊断问题吗?该文件跨网络本地化。这可能是一个潜在的问题。我间歇性地收到错误消息,“找不到路径的一部分......”和“指定的网络名称不再可用。”
采纳答案by Patrick McDonald
Alright, so I figured it out. My local machine was having problems intermittently accessing the file over the network. I uploaded the code to the server and ran it there without any problems. I really appreciate all the help. I'm sorry the solution wasn't very exciting.
好吧,所以我想通了。我的本地机器在通过网络访问文件时出现间歇性问题。我将代码上传到服务器并在那里运行,没有任何问题。我真的很感谢所有的帮助。很抱歉,解决方案不是很令人兴奋。
回答by Marcin Deptu?a
Your code works for me, after 3 runs I've:
你的代码对我有用,运行 3 次后我有:
start data data data
Are you sure, that your not trying access file, that for some reason you can't write to it? Also, just to make sure, place writer.Close()
before disposing it, although Dispose()
should flush the data. If this won't help, try to create the file manually using File.Create()
with appropriate flags.
你确定,你没有尝试访问文件,因为某种原因你不能写入它?另外,为了确保writer.Close()
在处理之前放置,尽管Dispose()
应该刷新数据。如果这没有帮助,请尝试使用File.Create()
适当的标志手动创建文件。
//Edit: I've tried this code on my machine:
//编辑:我在我的机器上试过这个代码:
public unsafe static void Main() { string filePath = @"\COMP-NAME\Documents\foo.txt"; FileStream fs = null; if (!File.Exists(filePath)) fs = File.Create(filePath); else fs = File.Open(filePath, FileMode.Append); using (StreamWriter writer = new StreamWriter(fs)) { writer.WriteLine("data"); } }
And it runs smoothly, could try this one?
而且运行流畅,可以试试这个吗?
回答by BoxOfNotGoodery
Your code worked correctly for me..
你的代码对我来说工作正常..
Try a using statement
尝试使用语句
using (StreamWriter writer = new StreamWriter(filePath, true))
{
if (!fileExists)
{
writer.WriteLine("start");
}
writer.WriteLine("data");
writer.Flush();
}
回答by Vincent Tan
The code ran fine on my computer. Can we know what the variable filePath contains? Perhaps you were looking at the wrong file...
代码在我的电脑上运行良好。我们能知道变量 filePath 包含什么吗?也许您正在查看错误的文件...
UPDATE: Network problem? Maybe someone was doing something on the other side of the network. Try writing to a local file. If it works, try writing to a remote file on another location.
更新:网络问题?也许有人在网络的另一端做某事。尝试写入本地文件。如果可行,请尝试写入另一个位置的远程文件。
回答by SwDevMan81
回答by Patrick McDonald
Similarly to others who have posted, your code is working fine on my PC.
与其他已发布的人类似,您的代码在我的 PC 上运行良好。
It might help for you to break your code in two parts, the first part writing the header if the file doesn't already exist, then the second part writing the data:
将代码分为两部分可能对您有所帮助,如果文件不存在,则第一部分写入标头,然后第二部分写入数据:
try
{
using (FileStream fs = new FileStream(filePath, FileMode.CreateNew))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("start");
}
}
}
catch (IOException ex)
{
}
using (StreamWriter writer = new StreamWriter(filePath, true))
{
writer.WriteLine("data");
}