我每隔几秒钟运行一个批处理文件,以使用以下代码与服务器进行时间同步:
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 文件。