Question

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.

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."

Was it helpful?

Solution 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.

OTHER TIPS

The code ran fine on my computer. Can we know what the variable filePath contains? Perhaps you were looking at the wrong file...

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.

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

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

Similarly to others who have posted, your code is working fine on my 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");
}

Your code works for me, after 3 runs I've:

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.

//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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top