我如何启动应用程序并立即在C#中关闭它
作为自动测试用例的一部分,我必须启动一个应用程序(例如记事本),并在没有任何用户干预的情况下将其关闭。我用来启动此应用程序的可执行文件应在系统帐户中调用并实现这一目标,我使用PSEXEC实用程序。 So the call looks somewhat like this :
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "PsExec.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = " -i -s " + myExecutable.exe + " -application notepad.exe";
Process exeProcess = Process.Start(startInfo)
exeProcess.WaitForExit();
The above code starts notepad and waits until it is closed, only then will I get a proper exit code from myExecutable.exe
Some of the ways that I tried :
Using timer with WaitForExit and Kill, this doesn 't封闭记事本,但分别以0和-1的形式存在该程序,也许是因为我正在杀死PSEXEC的过程,而不是可以杀死MyExecut的过程。
有没有办法自动关闭Notepad.exe?甚至可以用来在系统帐户中运行过程的API(因此我可以完全摆脱PSEXEC)。
As part of a automated test case, I have to start an application(for example notepad) and close it without any user intervention. The executable I use to launch this application should be called in System account and to achieve this I use PSExec utility. So the call looks somewhat like this :
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.FileName = "PsExec.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = " -i -s " + myExecutable.exe + " -application notepad.exe";
Process exeProcess = Process.Start(startInfo)
exeProcess.WaitForExit();
The above code starts notepad and waits until it is closed, only then will I get a proper exit code from myExecutable.exe
Some of the ways that I tried :
Using timer with WaitForExit and Kill, this doesn't close notepad but exists the program with 0 and -1 respectively, maybe because I'm killing the process PSExec and not myexecutable.
Is there a way to close Notepad.exe automatically? Or even an API that can be used to run a process in System account(So I can get rid of PSExec entirely).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论