Domanda

I have the following snippet of code, which takes lines from a List and writes them off to disk:

List<string> messages; // a list full of lines of text to write to file
using (System.IO.TextWriter writer = new System.IO.StreamWriter(stSessionStatusFile, true))
{
   int i = 0;
   while (i < messages.Count)
   {
      string line = messages[0];
      writer.WriteLine(line);
      writer.Flush();
      messages.RemoveAt(0);
   }
}

However... the resulting file is not created on disk, nor are there any exceptions thrown. It's like the text to write to disk just disappears into thin air. This code works in another assembly, just not in the service that I ported it to. I have no idea why the text wouldn't be written to disk in this case, many thanks for any help with solving this problem.

È stato utile?

Soluzione 4

I rebooted and everything started working as expected. I don't really have an explanation as to what happened, but I'd guess that my PC is the issue here, not coder error. Thanks for the help guys

Altri suggerimenti

Have you considered using File.WriteAllLines? http://msdn.microsoft.com/en-us/library/dd383693.aspx. My experience it's much cleaner for these types of operations. Helps narrow down the potential for errors.

I think your i is not incrementing. Do i++ inside the loop. Also do this. foreach line, write line, outside the loop flush. also remove the removeat

foreach(string line in messages) {
  writer.WriteLine(line)
}

writer.Flush();

Check the credential that you used to run the service ... Maybe you could try writing into some temp folder (e.g. C:\Temp) and see if it works

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top