Program.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Diagnostics;
  3. using System.Reflection;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6. namespace prBrowser
  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[] processes = Process.GetProcessesByName(current.ProcessName);
  29. Process[] array = processes;
  30. foreach (Process process in array)
  31. {
  32. if (process.Id != current.Id && Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
  33. {
  34. return process;
  35. }
  36. }
  37. return null;
  38. }
  39. private static void HandleRunningInstance(Process instance)
  40. {
  41. ShowWindowAsync(instance.MainWindowHandle, 3);
  42. SetForegroundWindow(instance.MainWindowHandle);
  43. }
  44. [DllImport("User32.dll")]
  45. private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
  46. [DllImport("User32.dll")]
  47. private static extern bool SetForegroundWindow(IntPtr hWnd);
  48. }
  49. }