简体   繁体   English

如何从C#中的第一个程序打开另一个程序?

[英]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 这将使用默认值forcefalse ,并且不会强制将其关闭


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文件,请参见以下内容: codeprojectstackoverflow

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. 我立刻想到两种方法。

  • The first is to ungracefully kill the exe as a process. 首先是作为进程恶意删除exe。 You can kill any exe either by id or it's exe-name. 您可以通过id或exe名称杀死任何exe。
  • The second one is to set up a communication between two processes with the help of WCF for interprocess communication. 第二个方法是在WCF的帮助下在两个进程之间建立通信,以进行进程间通信。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM