0

当我调用此函数时,它只会打开几个空的记事本窗口,但如果我text2在运行后打开文件,它会显示所有输入的行...我什至尝试刷新以写入缓冲区中的所有内容,但没有帮助。

static void finish()
{
    FileStream fs2 = new FileStream("C:\\Users\\ayush.mishra\\Desktop\\text2.txt", FileMode.Create, FileAccess.Write);
    using(StreamWriter s = new StreamWriter(fs2))
    {
        s.WriteLine("This Notepad will self destruct in....");
        fs2.Flush();
        Process notepadProcess2 = Process.Start("C:\\Users\\ayush.mishra\\Desktop\\text2.txt");
        for (var i = 0; i < 40000000.00; i++);
        notepadProcess2.CloseMainWindow();
        s.WriteLine(" ******  5  ****** ");
        fs2.Flush();
        notepadProcess2 = Process.Start("C:\\Users\\ayush.mishra\\Desktop\\text2.txt");
        for (var i = 0; i < 400000000; i++);
        notepadProcess2.CloseMainWindow();
        s.WriteLine(" ******  4  ****** ");
        fs2.Flush();
        notepadProcess2 = Process.Start("C:\\Users\\ayush.mishra\\Desktop\\text2.txt");
        for (var i = 0; i < 400000000; i++);
        notepadProcess2.CloseMainWindow();
        s.WriteLine(" ******  3  ****** ");
        fs2.Flush();
        notepadProcess2 = Process.Start("C:\\Users\\ayush.mishra\\Desktop\\text2.txt");
        for (var i = 0; i < 400000000; i++);
        notepadProcess2.CloseMainWindow();
        notepadProcess2 = Process.Start("C:\\Users\\ayush.mishra\\Desktop\\text2.txt");
        s.WriteLine(" ******  2  ****** ");
        fs2.Flush();
        for (var i = 0; i < 400000000; i++);
        notepadProcess2.CloseMainWindow();
        notepadProcess2 = Process.Start("C:\\Users\\ayush.mishra\\Desktop\\text2.txt");
        s.WriteLine(" ******  1  ****** ");
        fs2.Flush();
        for (var i = 0; i < 400000000; i++);
        notepadProcess2.CloseMainWindow();
    }
}

有什么建议么?

4

1 回答 1

1

The problem here is that you are opening the file first in notepad and then writing data to it. Since file is already opened in notepad so it will not show changes. But if you open file again in notepad it will display data written to it.

于 2013-06-26T09:04:24.193 回答