using System;
using System.Diagnostics;
using System.Collections;
using System.Management;
using System.IO;
FileStream ostrm;
StreamWriter writer;
TextWriter oldOut = Console.Out;
try
{
ostrm = new FileStream ("./Output.txt", FileMode.OpenOrCreate, FileAccess.Write);
writer = new StreamWriter (ostrm);
}
catch (Exception e)
{
Console.WriteLine("Cannot open Output for writing");
Console.WriteLine(e.Message);
return;
}
foreach (ManagementObject envVar in searcher.Get())
{
Console.WriteLine("Username: {0}", envVar["Name"]);
}
Console.SetOut (writer);
foreach (ManagementObject envVar in searcher.Get())
{
Console.WriteLine("Username: {0}", envVar["Name"]);
}
Console.SetOut (oldOut);
writer.Close();
ostrm.Close();
Console.WriteLine("" +
Environment.NewLine + "Press any key to exit");
Console.ReadLine();
我在控制台应用程序中有这段代码,它需要 AGES 才能完成写入网络上的所有用户,如果它在任何阶段被中断,Output.txt 最终会是空的。有没有办法让它定期保存,以便如果发生任何事情我在文本文件中有用户名?
任何帮助是极大的赞赏。