我如何启动应用程序并立即在C#中关闭它

发布于 2025-01-28 08:35:23 字数 951 浏览 1 评论 0原文

作为自动测试用例的一部分,我必须启动一个应用程序(例如记事本),并在没有任何用户干预的情况下将其关闭。我用来启动此应用程序的可执行文件应在系统帐户中调用并实现这一目标,我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文