革博士程序V1仓库
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

80 Zeilen
3.0 KiB

  1. using LeatherApp.Utils;
  2. using Sunny.UI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace LeatherApp
  12. {
  13. internal static class Program
  14. {
  15. /// <summary>
  16. /// 应用程序的主入口点。
  17. /// </summary>
  18. [STAThread]
  19. static void Main(string[] sp)
  20. {
  21. //byte[] buff = File.ReadAllBytes(@"D:\AZTCode\HBH\证书\测试环境SSL\国际根证书\国际根证书.cer");
  22. //string base64=Convert.ToBase64String(buff);
  23. Process instance = RunningInstance();
  24. if (instance == null)
  25. {
  26. Application.ThreadException += Application_ThreadException;
  27. Application.EnableVisualStyles();
  28. Application.SetCompatibleTextRenderingDefault(false);
  29. Application.Run(new FrmMain());
  30. }
  31. else
  32. {
  33. MessageBox.Show("当前程序已经运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, 0);
  34. }
  35. }
  36. //不允许有两个程序同时启动
  37. public static Process RunningInstance()
  38. {
  39. Process current = Process.GetCurrentProcess();
  40. Process[] processes = Process.GetProcessesByName(current.ProcessName);
  41. //遍历正在有相同名字运行的例程
  42. foreach (Process process in processes)
  43. {
  44. //忽略现有的例程
  45. if (process.Id != current.Id)
  46. {
  47. //确保例程从EXE文件运行
  48. if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") ==
  49. current.MainModule.FileName)
  50. {
  51. //返回另一个例程实例
  52. return process;
  53. }
  54. }
  55. }
  56. //没有其它的例程,返回Null
  57. return null;
  58. }
  59. static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
  60. {
  61. Exception ex = e.Exception;
  62. using (StreamWriter sw = new StreamWriter(Directory.GetCurrentDirectory() + "\\ErrorLog.txt", true))
  63. {
  64. sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  65. sw.WriteLine("Global捕获到未处理异常:" + ex.GetType().ToString());
  66. sw.WriteLine("异常信息:" + ex.Message);
  67. sw.WriteLine("异常堆栈:" + ex.StackTrace);
  68. sw.WriteLine();
  69. }
  70. MessageBox.Show(string.Format("捕获到未处理异常:{0}\r\n异常信息:{1}\r\n异常堆栈:{2}", ex.GetType(), ex.Message, ex.StackTrace));
  71. }
  72. }
  73. }