views:

1651

answers:

6

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

A: 

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?

Ravadre
That was my first reaction. I tried creating the file if it didn't exist and just using the above code, but that also would write on the first call. Are there any latency issues with creating a file on a server this way?
I've posted code that I've cooked to check if this will work over network, under Edit section. Don't treat it as a production code btw, cause of not 100% correct usage of using this time, but it works for me, it's putting data when creating file and then appending to id.
Ravadre
A: 

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();
}
JohnFly
The author is using `using` statement?
Ravadre
If you look at the sample code, I am using a using statement?
+1  A: 

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.

Vincent Tan
string filePath = @"\\server\folder\filename.htm";It works on subsequent calls, after the file is created so I know it is the right path.
Based on the intermittent error messages you gave, the server itself could be busy/offline. Or the folder itself was deleted, and you don't have permission to create the folder (or haven't created the folder). A file/folder permission issue perhaps?
Vincent Tan
A: 

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

SwDevMan81
A: 

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");
}
Patrick McDonald
+1  A: 

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.