tags:

views:

84

answers:

3

I have a program that looks something like this:

public partial class It
{
    static StreamWriter logging 
       = new StreamWriter(new FileStream(@"C:\log",FileMode.Create));

    void someFn()
    {
       logging.WriteLine("foo");
       logging.Flush();

       /// killed here in debugger with Shift+F5
    }
}

The problem it that the file doesn't end in "foo" and it seems the flush isn't happening. Am I abusing something here? I need a "the bits are in the file when I return" function, does such a thing exist?

Ideal would be that if I break at that point, another process will be bale to see that last line written.

+1  A: 

I think your code ought to work as you expect. I wonder if you have the build mode set to Release and/or there is a discrepancy between the source code you are seeing in the debugger and the code that is actually be executed. If the project is set to build in Release mode, the optimizer may be moving some code around and the source line that you are on may not represent exactly what is happening in the optimized code.

tvanfosson
I really hope that's not the kind of issue I'm getting...
BCS
+2  A: 

I remember I got this problem too. I think you have to flush the underlying FileStream rather than the StreamWriter

oykuo
Fud :( that's going to be a pain.
BCS
No, joy. didn't get what I need either. (I can't repro exactly what I described but it's still not working right)
BCS
oykuo
I think that was the most helpful. It turned into one of those "it works now so who cares" issues
BCS
A: 

Can you, please, try this code and let us know results:

class Program
{
    static void Main(string[] args)
    {
        StreamWriter foo = new StreamWriter(new FileStream(@"C:\foo.txt", FileMode.Create));
        foo.WriteLine("foo");

        StreamWriter bar = new StreamWriter(new FileStream(@"C:\bar.txt", FileMode.Create));
        bar.WriteLine("bar");
        bar.Flush();
    }
}
alex2k8