rd42
rd42

Reputation: 3594

C# StreamWriter not writing to file

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

Upvotes: 2

Views: 11758

Answers (4)

Henk Holterman
Henk Holterman

Reputation: 273701

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

Upvotes: 5

Shai
Shai

Reputation: 25595

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

Upvotes: 2

Steve
Steve

Reputation: 216343

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 :-)

Upvotes: 4

ionden
ionden

Reputation: 12786

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

Upvotes: 2

Related Questions