StreamWriter - 不附加到创建的文件

时间:2021-02-22 21:18:24

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块负责关闭writer。至于其他人说它适合他们,有什么信息我可以给你进一步诊断问题吗?该文件通过网络定位。这可能是一个潜在的问题。我不断地收到错误,“无法找到路径的一部分...”和“指定的网络名称不再可用”。

6 个解决方案

#1


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.

更新:网络问题?也许某人正在网络的另一端做某事。尝试写入本地文件。如果有效,请尝试写入另一个位置的远程文件。

#2


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.

好吧,我弄清楚了。我的本地计算机在通过网络间歇性地访问文件时遇到问题。我将代码上传到服务器并在那里运行它没有任何问题。我非常感谢所有的帮助。对不起解决方案不是很令人兴奋。

#3


Could try the File.Open function. There is a good example at the bottom and list of the FileModes FileMode.Append is what you would want

可以尝试File.Open函数。底部有一个很好的例子,FileMode FileMode.Append列表就是你想要的

#4


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();
}

#5


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");
}

#6


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?

它运行顺利,可以尝试这个吗?

#1


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.

更新:网络问题?也许某人正在网络的另一端做某事。尝试写入本地文件。如果有效,请尝试写入另一个位置的远程文件。

#2


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.

好吧,我弄清楚了。我的本地计算机在通过网络间歇性地访问文件时遇到问题。我将代码上传到服务器并在那里运行它没有任何问题。我非常感谢所有的帮助。对不起解决方案不是很令人兴奋。

#3


Could try the File.Open function. There is a good example at the bottom and list of the FileModes FileMode.Append is what you would want

可以尝试File.Open函数。底部有一个很好的例子,FileMode FileMode.Append列表就是你想要的

#4


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();
}

#5


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");
}

#6


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?

它运行顺利,可以尝试这个吗?