我每隔几秒钟就运行一个批处理文件,使用以下代码与服务器进行时间同步:
Process process = new Process();
process.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);
process.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe");
process.StartInfo.Arguments = @"/C C:\TimeSync.bat";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UserName = "username";
SecureString pwd = new SecureString();
Char[] pwdCharacters = "password".ToCharArray();
foreach (char t in pwdCharacters)
{
pwd.AppendChar(t);
}
process.StartInfo.Password = pwd;
process.Start();
string output = process.StandardOutput.ReadToEnd();
问题是它会在屏幕上闪烁命令窗口,这不是我想要的。我该如何防止这种情况发生?
我观察到的一种行为是,如果我使用
UseShellExecute = true
运行相同的代码,并且不提供用户名和密码,则命令窗口不会闪烁。因此,基本上我想使用C#代码以不同的用户静默方式运行.bat文件。
info.WindowStyle = ProcessWindowStyle.Hidden
,但我不确定这是否适用于控制台应用程序。 - Uwe Keim