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