Question

So the problem is this I declare the variables and open the stream at the begining of the method:

    int i = 0;
    int FailedToCopyImages = 0;
    int NumberOfCopiedImages = 0;
    int PreviouslyCopiedImages = 0;
    TextWriter tw = new StreamWriter(pathToFile, true);

Then I do some stuff in try-catch-finally block and in the `finally part I have :

finally
            {
                Console.WriteLine(i);
                Console.WriteLine(NumberOfCopiedImages);
                Console.WriteLine(PreviouslyCopiedImages);
                Console.WriteLine(FailedToCopyImages);
                tw.WriteLine(" ");
                tw.WriteLine("All images: " + i +
                    " | Successfully copied: " + NumberOfCopiedImages +
                    " | Previously copied: " + PreviouslyCopiedImages +
                    " | Failed To Copy: " + FailedToCopyImages);
                tw.WriteLine("--------------End Of Material Images-------------");

I do this in four methods and I get the right results there. Here in the Console I see that the variables holds the correct value but in the txt file I get zeroes (0).

Was it helpful?

Solution

In most cases you need to call tw.Flush() and tw.Close() (or just wrap new StreamWriter(..) in using (...) statement) before seeing results in file.

Even if it seems to start working with other file path (as you commented) - there's no magic (at least in programming) - and it will fail someday without proper Flush / Dispose.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top