如何在 C# 中从另一个启动应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1112981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:32:26  来源:igfitidea点击:

How do I launch application one from another in C#?

c#desktop-applicationlaunch

提问by Suriyan Suresh

I have two desktop applications. After closing the first application, the first application will start the second application.

我有两个桌面应用程序。关闭第一个应用程序后,第一个应用程序将启动第二个应用程序。

How do I start the second application after finishing first application?

完成第一次申请后如何开始第二次申请?

My first application creates a separate desktop.

我的第一个应用程序创建了一个单独的桌面。

采纳答案by Chansik Im

You can use .NET's Process Class to start a process as other people described. Then the question is when to call.

您可以使用 .NET 的 Process Class 来启动其他人所描述的进程。那么问题是什么时候打电话。

In most cases, using either Form.Closingor Form.Closedevent seems to be an easy choice.

在大多数情况下,使用Form.ClosingForm.Closed事件似乎是一个简单的选择。

However, if someone else can handle the event and can set CancelEventArgs.Cancelto true, this may not be the right place to do this. Also, Form.Closingand Form.Closedevents will not be raised when Application.Exit()is called. I am not sure whether either of events will be raised if any unhandled exceptions occur. (Also, you have to decide whether you want to launch the second application in case of Application.Exit()or any unhandled exception).

但是,如果其他人可以处理该事件并且可以设置CancelEventArgs.Cancel为 true,那么这可能不是执行此操作的正确位置。此外,Form.ClosingForm.Closed当事件将不会引发 Application.Exit()被调用。如果发生任何未处理的异常,我不确定是否会引发任何一个事件。(此外,您必须决定是否要启动第二个应用程序以防万一Application.Exit()或任何未处理的异常)。

If you really want to make sure the second application (App2) launches after the first application (App1) exited, you can play a trick:

如果你真的想确保第二个应用程序 (App2) 在第一个应用程序 (App1) 退出后启动,你可以玩一个技巧:

  1. Create a separate application (App0)
  2. App0 launches App1
  3. App0 waits for App1 to exit with Process.WaitExit()
  4. App0 launches App2 and exits itself
  1. 创建单独的应用程序 (App0)
  2. App0 启动 App1
  3. App0 使用 Process.WaitExit() 等待 App1 退出
  4. App0 启动 App2 并自行退出

The sample console app attached below shows a very simple case: my sample app launches the notepad first. Then, when the notepad exits, it launches mspaint and exits itself.

下面附带的示例控制台应用程序显示了一个非常简单的案例:我的示例应用程序首先启动记事本。然后,当记事本退出时,它会启动 mspaint 并自行退出。

If you want to hide the console, you can simply set the 'Output Type' property from 'Console Application' to 'Windows Application' under 'Application' tab of Project Property.

如果您想隐藏控制台,您可以简单地将“输出类型”属性从“控制台应用程序”设置为“项目属性”的“应用程序”选项卡下的“Windows 应用程序”。

Sample code:

示例代码:

using System;
using System.Diagnostics;

namespace ProcessExitSample
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                Process firstProc = new Process();
                firstProc.StartInfo.FileName = "notepad.exe";
                firstProc.EnableRaisingEvents = true;

                firstProc.Start();

                firstProc.WaitForExit();

                //You may want to perform different actions depending on the exit code.
                Console.WriteLine("First process exited: " + firstProc.ExitCode);

                Process secondProc = new Process();
                secondProc.StartInfo.FileName = "mspaint.exe";
                secondProc.Start();                

            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred!!!: " + ex.Message);
                return;
            }
        }
    }
}

回答by JP Alioto

Use the Process classwhen you are exiting your first application.

退出第一个应用程序时使用Process 类

var p = new Process();
p.StartInfo.FileName   = "notepad.exe";  // just for example, you can use yours.
p.Start();

回答by Quintin Robinson

You could just shell off to it, so when you are about to exit the first app just start the second app via:

您可以直接使用它,因此当您要退出第一个应用程序时,只需通过以下方式启动第二个应用程序:

System.Diagnostics.Process.Start(@"PATH\NAME.EXE");

回答by Mitch Wheat

Use .NET's Processclass.

使用 .NET 的Process类。

回答by Hassan Abbas

Some sample code:

一些示例代码:

try
{
  stateMainLayout b = new stateMainLayout();
 b.Location = Screen.AllScreens[1].WorkingArea.Location;
 b.ShowDialog();
 }
catch
{
 stateMainLayout b = new stateMainLayout();
b.ShowDialog();
}

回答by huse.ckr

CSharp/PowerShell Calling Another Program and Send/Recieve Data: https://huseyincakir.wordpress.com/2014/12/23/sending-input-from-csharppowershell-to-another-program/

CSharp/PowerShell 调用另一个程序并发送/接收数据:https://huseyincakir.wordpress.com/2014/12/23/sending-input-from-csharppowershell-to-another-program/

回答by Mamo Ghandi

in Some cases its necessary to add Working directoryto your code in order to make the app works perfectly. especially when the app has dependency to DLL and other resources.

在某些情况下,有必要将工作目录添加 到您的代码中,以使应用程序完美运行。特别是当应用程序依赖于 DLL 和其他资源时。

 TestProcess.StartInfo.FileName = "notepad.exe"; 
 TestProcess.StartInfo.WorkingDirectory = @"C:\blah\blah\Directory of notepad.exe\";
 TestProcess.Start();

回答by Abdul Moiz

Here ProcName means the name of the application you want to start but it can only start system application and some other application

这里 ProcName 表示你要启动的应用程序的名称,但它只能启动系统应用程序和其他一些应用程序

        public void Startapp(String ProcName)
        {
            try
            {
                Process firstProc = new Process();
                firstProc.StartInfo.FileName = ProcName;
                firstProc.EnableRaisingEvents = true;
                firstProc.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }