在 C# 中重新启动应用程序

Muhammad Maisam Abbas 2024年2月16日
在 C# 中重新启动应用程序

本教程将讨论使用 C# 重新启动应用程序的方法。

使用 C# 中的 Application.Restart() 函数重新启动应用程序

在 C# 中重新启动应用程序的最简单方法是使用 Application.Restart() 函数。Application.Restart() 函数用于重新启动 C# 中的应用程序。下面的代码示例向我们展示了如何使用 Application.Restart() 函数来重新启动 C# 中的应用程序。

using System.Windows.Forms;

namespace messagbox {
  static class Program {
    static void Main() {
      Application.EnableVisualStyles();
      DialogResult dr = MessageBox.Show("Are you happy now?", "Mood Test", MessageBoxButtons.YesNo);
      switch (dr) {
        case DialogResult.Yes:
          MessageBox.Show("Ok, Bye");
          break;
        case DialogResult.No:
          Application.Restart();
          break;
      }
    }
  }
}

输出:

C# 重新启动应用程序
C# 重新启动应用程序

在上面的代码中,我们创建了一个应用程序,该应用程序将自行重启,直到使用 C# 中的 Application.Restart() 函数从消息框中选择为止。

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn