我正在使用特殊用户,域名和密码启动流程。虽然我告诉C#隐藏控制台窗口,但它会显示出来。
这是我的代码:
Process process = new Process();
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo.UserName = strUsername;
process.StartInfo.Domain = strDomain;
process.StartInfo.Password = secPassword;
process.StartInfo.FileName = "PsExec.exe";
process.StartInfo.Arguments = @"/accepteula -s \\" + strServername + @"program.exe";
process.Start();
process.WaitForExit();
我可以在另一个论坛找到一些提示:
如果使用。调用Start(ProcessStartInfo)方法 ProcessStartInfo .. ::。UserName和ProcessStartInfo .. ::。Password 属性设置,非托管的CreateProcessWithLogonW函数是 调用,即使是在新窗口中启动该过程 CreateNoWindow属性值为true或WindowStyle属性 值是隐藏的。
实际上,我对此声明并不满意......
提前致谢。
干杯 亚历
答案 0 :(得分:1)
据我所知,这个问题有解决方法。您可以使用参数启动隐藏的cmd。像这样:
ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", String.Format("/accepteula -s \\{0}program.exe", strServername));
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process.Start(psi);
答案 1 :(得分:1)
using (Process LMUTIL = new Process())
{
string arg1 ="argument"
LMUTIL.StartInfo.FileName = "program.exe";
LMUTIL.StartInfo.Arguments = arg1
LMUTIL.StartInfo.UseShellExecute = false;
LMUTIL.StartInfo.RedirectStandardOutput = true;
LMUTIL.StartInfo.CreateNoWindow = true;
LMUTIL.EnableRaisingEvents = true;
LMUTIL.OutputDataReceived += p_WriteData;
LMUTIL.Start();
LMUTIL.BeginOutputReadLine();
}
private void p_WriteData(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
Debug.WriteLine(e.Data.ToString());
}
}
我直接从一个能满足你需要的项目中解脱出来。订阅p_WriteData事件以捕获命令窗口中显示的内容。
答案 2 :(得分:1)
毕竟,无法使用Process Class隐藏此psexec控制台窗口。 没有必要使用psexec。我改用了WMI:
ConnectionOptions remoteConnectionOptions = new ConnectionOptions();
remoteConnectionOptions.Impersonation = ImpersonationLevel.Impersonate;
remoteConnectionOptions.EnablePrivileges = true;
remoteConnectionOptions.Authentication = AuthenticationLevel.Packet;
remoteConnectionOptions.Username = strDomain + @"\" + strUsername;
remoteConnectionOptions.SecurePassword = secPassword;
ManagementScope scope = new ManagementScope(@"\\" + strServername + @"\root\CIMV2", remoteConnectionOptions); ManagementPath p = new ManagementPath("Win32_Process");
ManagementClass classInstance = new ManagementClass(scope, p, null); object[] theProcessToRun = { "myExecutable.exe" };
classInstance.InvokeMethod("Create", theProcessToRun);