当 WinFormApp 将要关闭时,如何在 C# 中启动另一个 .exe?

How to start another .exe in C# when the WinFormApp is going to be closed?

当一个用C#写的WinFromApp.exe关闭时, 它终于应该开始另一个 Prog.exe.

重要的是,WinFromApp.exe 不应该等待 Prog.exe 的结果。 WinFromApp.exe 应该在 Prog.exe 运行时完全关闭。

[这是我第二次使用这个论坛来提问。]

我仍然用 Dreamweaver MS-Office 编写了很多程序-VBA...

Visual Studio (2017) 编程对我来说是新的。因此,我还需要知道 code/code-part(s).

的放置位置

我有来自"Welcome.cs" [代码中的“++...++”标记完成,包括来自 Anu Viswan。]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using Microsoft.VisualBasic;
using Microsoft.Win32;
using HardwareHelperLib;

namespace Welcome
{
    public partial class Welcome : Form
    {

        public Welcome()
        {
            InitializeComponent();
            +Application.ApplicationExit += Application_ApplicationExit;+
        }

        private void wb_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            ...;
        }

        private void wb_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {
            ...;
        }

        private void Welcome_Load(object sender, EventArgs e)
        {

            ...;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ...;
        }

        ++private void Application_ApplicationExit(object sender, EventArgs e)
        {
            var process = new ProcessStartInfo("notepad.exe");
            Process.Start(process);
        }
        private void Welcome_FormClosing(object sender, FormClosingEventArgs e)
        {
            var process = new ProcessStartInfo("notepad.exe");
            Process.Start(process);
        }++
    }
}

和Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;

namespace Welcome
{
    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Welcome());
        }
    }
}

我必须放置这个:

Application.ApplicationExit += Application_ApplicationExit;

在这里 [+...+],它正在工作 = 非常感谢你 !!!

This question already has an answer here: How to run code before program exit? [duplicate]

I have looked at this link, but not found the answer of my Question...

Because as I has written in my Question:

Visual Studio (2017) programming is new for me. Because of this, I need also to know where to place the code/code-part(s).

Sorry, until yesterday the last years I used Whosebug only as a Reader.

So I am also new at Whosebug doing Questions.

假设您的应用程序是 WinForm 应用程序,您可以使用 ApplicationExit 事件执行此操作。

首先您需要订阅应用程序退出和 FormClosing 事件。

Application.ApplicationExit += Application_ApplicationExit;

然后,在这种情况下,您可以使用 Process.Start 调用第二个 exe。

private void Application_ApplicationExit(object sender, EventArgs e)
{
    var process = new ProcessStartInfo("notepad.exe");
    Process.Start(process);
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
    var process = new ProcessStartInfo("notepad.exe");
    Process.Start(process);
}