C# StreamWriter not writing to file

11,703

Solution 1

Ooops there is a semicolon at the end of the using line???

Perhaps your intention was:

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\\sorted.txt", true))
{
    foreach (BasicLog basicLog in emailAttach) 
    {          
        file.WriteLine(basicLog.LastName + " - " + basicLog.InOrOut + " - " + basicLog.EventTime + "\n"); 
    } 
}

Curious, the same thing happened to me this morning :-)

Solution 2

Yes, you do have a mistaken empty statement.

remove the ';' and indent to show why:

foreach (BasicLog basicLog in emailAttach)
{
   using (System.IO.StreamWriter file = 
            new System.IO.StreamWriter(@"C:\\sorted.txt", true))  //;
   {
      file.WriteLine(basicLog.LastName + " - " + basicLog.InOrOut + " - "
             + basicLog.EventTime + "\n");
   }
}

The WriteLine() statement is (should be) under the control of the using(). The {} make that clearer.

This will work but note that it is very inefficient, you are reopening the file (for append) multiple times.

So it is better to invert the foreach/using:

using (System.IO.StreamWriter file = 
      new System.IO.StreamWriter(@"C:\\sorted.txt", true)) 
{
   foreach (BasicLog basicLog in emailAttach)
   {
        file.WriteLine(basicLog.LastName + " - " + basicLog.InOrOut + " - " 
            + basicLog.EventTime + "\n");
   }
}

Solution 3

Your file instance will get Disposed right after this line (Since you're using using):

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\\sorted.txt", true));

Change it to

System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\\sorted.txt", true);

Or the full code,

System.IO.StreamWriter file;
foreach (BasicLog basicLog in emailAttach)
{
    file = new System.IO.StreamWriter(@"C:\\sorted.txt", true);
    file.WriteLine(basicLog.LastName + " - " + basicLog.InOrOut + " - " + basicLog.EventTime + "\n");
}

Or make a correct use of using

foreach (BasicLog basicLog in emailAttach)
{
    using(System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\\sorted.txt", true))
    {
        file.WriteLine(basicLog.LastName + " - " + basicLog.InOrOut + " - " + basicLog.EventTime + "\n");
    }
}

Solution 4

You are disposing the StreamWriter before writing to the file. Refactor to this:

using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\\sorted.txt", true))   
{
   foreach (BasicLog basicLog in emailAttach)
   {     
      file.WriteLine(basicLog.LastName + " - " + basicLog.InOrOut + " - " + basicLog.EventTime + "\n");
   }
}
Share:
11,703
rd42
Author by

rd42

Updated on October 16, 2022

Comments

  • rd42
    rd42 over 1 year

    basicLog is a list of names and timestamps. I want to write them to a file. The two errors I'm getting are on the ';' on the StreamWriter line and on 'file.' on the second line.

    The ';' error is: Possible mistaken empty statement The error on file is: The name 'file' does not exist in the current context.

    The file gets created just fine, but nothing gets written to it. I'm confused about file not existing in current context, because on the line prior it get's created. Thank you for any help.

    foreach (BasicLog basicLog in emailAttach)
    {
        using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\\sorted.txt", true));
        file.WriteLine(basicLog.LastName + " - " + basicLog.InOrOut + " - " + basicLog.EventTime + "\n");
    }
    
  • Archeg
    Archeg over 12 years
    It's better to remove semicolon than remiving a using. Using is placed right here
  • Shai
    Shai over 12 years
    @Archeg this guy is not having a spare semicolon, he has a misplaced one, now go and explain that to him. Thing is hes MISUSING the using` directive
  • MiMo
    MiMo over 12 years
    @Archeg is right. If you don't use using when opening files and there is an exception the file will stay open, with all the possible related problems