Program.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using System;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6. namespace prBrowserUpdate
  7. {
  8. internal static class Program
  9. {
  10. [STAThread]
  11. private static void Main()
  12. {
  13. Application.EnableVisualStyles();
  14. Application.SetCompatibleTextRenderingDefault(defaultValue: false);
  15. Process instance = RunningInstance();
  16. if (instance == null)
  17. {
  18. Application.Run(new MainForm());
  19. }
  20. else
  21. {
  22. HandleRunningInstance(instance);
  23. }
  24. }
  25. private static Process RunningInstance()
  26. {
  27. Process current = Process.GetCurrentProcess();
  28. Process[] processesByName = Process.GetProcessesByName(current.ProcessName);
  29. foreach (Process process in processesByName)
  30. {
  31. if (process.Id != current.Id && Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
  32. {
  33. return process;
  34. }
  35. }
  36. return null;
  37. }
  38. private static void HandleRunningInstance(Process instance)
  39. {
  40. ShowWindowAsync(instance.MainWindowHandle, 3);
  41. SetForegroundWindow(instance.MainWindowHandle);
  42. }
  43. [DllImport("User32.dll")]
  44. private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
  45. [DllImport("User32.dll")]
  46. private static extern bool SetForegroundWindow(IntPtr hWnd);
  47. }
  48. }