所以问题是我声明变量并在方法开始时打开流:
int i = 0;
int FailedToCopyImages = 0;
int NumberOfCopiedImages = 0;
int PreviouslyCopiedImages = 0;
TextWriter tw = new StreamWriter(pathToFile, true);
然后我在try-catch-finally
块中做一些事情,在最后部分我有:
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-------------");
我用四种方法做到这一点,我在那里得到了正确的结果。在控制台中,我看到变量保持正确的值,但在 txt 文件中我得到零 (0)。