Program.cs 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Winsoft.GOV.WCF;
  8. namespace Winsoft.GOV.ConsoleHost
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. try
  15. {
  16. if (!IsExistProcess())
  17. {
  18. Host.Start();
  19. }
  20. else
  21. {
  22. System.Console.WriteLine(String.Format("'{0}'程序已经存在.", Process.GetCurrentProcess().ProcessName));
  23. }
  24. }
  25. finally
  26. {
  27. System.Console.ReadLine();
  28. }
  29. }
  30. /// <summary>
  31. /// Is exist same process
  32. /// </summary>
  33. /// <returns></returns>
  34. public static void KillExistProcess()
  35. {
  36. var current = Process.GetCurrentProcess();
  37. var pArrayy = System.Diagnostics.Process.GetProcessesByName(current.ProcessName);
  38. if (pArrayy == null || pArrayy.Length == 0)
  39. {
  40. return;
  41. }
  42. foreach (var item in pArrayy)
  43. {
  44. if (!item.ProcessName.ToUpper().Contains(current.ProcessName.ToUpper()))
  45. {
  46. continue;
  47. }
  48. if (item.Id != current.Id && item.MainModule.ModuleName.ToUpper() == current.MainModule.ModuleName.ToUpper())
  49. {
  50. item.Kill();
  51. }
  52. }
  53. }
  54. public static Boolean IsExistProcess()
  55. {
  56. var current = Process.GetCurrentProcess();
  57. var pArrayy = System.Diagnostics.Process.GetProcessesByName(current.ProcessName);
  58. if (pArrayy == null || pArrayy.Length == 0)
  59. {
  60. return false;
  61. }
  62. foreach (var item in pArrayy)
  63. {
  64. if (!item.ProcessName.ToUpper().Contains(current.ProcessName.ToUpper()))
  65. {
  66. continue;
  67. }
  68. if (item.Id != current.Id && item.MainModule.ModuleName.ToUpper() == current.MainModule.ModuleName.ToUpper())
  69. {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. }
  76. }