[英]How to open another program from my first in C#?
I want to open a second program from my first program and still be able to work on my first program. 我想从第一个程序中打开第二个程序,但仍然可以在第一个程序上工作。 And another thing is to close the second program from my first.
另一件事是从我的第一个关闭第二个程序。 Is there a way to do this?
有没有办法做到这一点?
For running the program: 对于运行程序:
You need using System.Diagnostics;
您需要
using System.Diagnostics;
Process open_exe(string path)
{
Process to_open;
to_open.StartInfo.FileName = path;
to_open.Start();
return (to_open);
}
For closing the program: 要关闭程序:
void close_exe(Process p, bool force = false)
{
if(force)
{
p.Kill();
}
else
{
p.CloseMainWindow();
}
}
When calling open_exe
, it returns a Process
, which you can use in the close_exe
function as the argument. 调用
open_exe
,它将返回一个Process
,您可以在close_exe
函数中将其用作参数。
Addition: on the close_exe
function, you can either call it by: 另外:在
close_exe
函数上,可以通过以下方式调用它:
close_exe(process);
close_exe(过程);
This will use the default value for force
as false
, and will not force it to close 这将使用默认值
force
为false
,并且不会强制将其关闭
close_exe(process, true);
close_exe(process,true);
This will not use the default value for force
, and use true
, and will thus force it to close 这将不会使用
force
的默认值,而是使用true
,从而强制将其关闭
As to launching another exe-file see the following: codeproject , stackoverflow 至于启动另一个exe文件,请参见以下内容: codeproject , stackoverflow
As to closing the application I would say that the way to close the app depends on your needs. 关于关闭应用程序,我会说关闭应用程序的方式取决于您的需求。 Two ways come to my mind immediately.
我立刻想到两种方法。
You can easily google about killing processes in C# as well as about setting up a communication between two processes. 您可以轻松地搜索有关C#中杀死进程以及建立两个进程之间的通信的信息。
您只需要一行即可(使用“使用System.Diagnostics”)
Process.Start(pathToAnotherProgramm);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.