C# winform 判斷程式是否已經在執行 判斷程式是否已經開啟
阿新 • • 發佈:2018-12-31
在 Program.cs 開啟的時候在裡面修改
[STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); if (IsRunning()) { MessageBox.Show("程式已經開啟!"); return; } Application.Run(new Main()); } public static bool IsRunning() { Process current = default(Process); current = System.Diagnostics.Process.GetCurrentProcess(); Process[] processes = null; processes = System.Diagnostics.Process.GetProcessesByName(current.ProcessName); Process process = default(Process); foreach (Process tempLoopVar_process in processes) { process = tempLoopVar_process; if (process.Id != current.Id) { if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName) { return true; } } } return false; }