在 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