简体   繁体   中英

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;

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.

Addition: on the close_exe function, you can either call it by:

close_exe(process);

This will use the default value for force as false , and will not force it to close


close_exe(process, true);

This will not use the default value for force , and use true , and will thus force it to close

As to launching another exe-file see the following: 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.

  • The first is to ungracefully kill the exe as a process. You can kill any exe either by id or it's exe-name.
  • The second one is to set up a communication between two processes with the help of WCF for interprocess communication.

You can easily google about killing processes in C# as well as about setting up a communication between two processes.

您只需要一行即可(使用“使用System.Diagnostics”)

 Process.Start(pathToAnotherProgramm);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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