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

发布于 2024-07-26 16:18:03 字数 110 浏览 6 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

蒲公英的约定 2024-08-02 16:18:03

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

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

Use the Process class when you are exiting your first application.

var p = new Process();
p.StartInfo.FileName   = "notepad.exe";  // just for example, you can use yours.
p.Start();
可可 2024-08-02 16:18:03

您可以使用 .NET 的进程类来启动进程,正如其他人所描述的那样。 那么问题就是什么时候打电话。

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

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

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

  1. 创建一个单独的应用程序 (App0)
  2. App0 启动 App1
  3. App0 等待 App1 退出 Process.WaitExit( )
  4. App0 启动 App2 并自行退出

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

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

示例代码:

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;
            }
        }
    }
}

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

In most cases, using either Form.Closing or Form.Closed event seems to be an easy choice.

However, if someone else can handle the event and can set CancelEventArgs.Cancel to true, this may not be the right place to do this. Also, Form.Closing and Form.Closed events 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).

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

  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

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.

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.

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;
            }
        }
    }
}
命比纸薄 2024-08-02 16:18:03

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

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

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");
欲拥i 2024-08-02 16:18:03

使用 .NET 的 进程 类。

Use .NET's Process class.

ペ泪落弦音 2024-08-02 16:18:03

一些示例代码:

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

Some sample code:

try
{
  stateMainLayout b = new stateMainLayout();
 b.Location = Screen.AllScreens[1].WorkingArea.Location;
 b.ShowDialog();
 }
catch
{
 stateMainLayout b = new stateMainLayout();
b.ShowDialog();
}
眼泪都笑了 2024-08-02 16:18:03

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

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

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

 TestProcess.StartInfo.FileName = "notepad.exe"; 
 TestProcess.StartInfo.WorkingDirectory = @"C:\\blah\blah\Directory of notepad.exe\";
 TestProcess.Start();
孤君无依 2024-08-02 16:18:03

这里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);
            }
        }

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

        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);
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文