using System; using System.IO; using System.Windows.Forms; namespace C_Windows_1 { static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); Application.ThreadException += Application_ThreadException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { // 获取项目的 Debug 目录路径 string logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "error.txt"); using (FileStream efs = new FileStream(logFilePath, FileMode.Append)) { byte[] data = System.Text.Encoding.Default.GetBytes("CurrentDomain_UnhandledException::" + e.ToString() + ",object:" + e.ExceptionObject.ToString()); efs.Write(data, 0, data.Length); } } private static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { // 获取项目的 Debug 目录路径 string logFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "error.txt"); using (FileStream efs = new FileStream(logFilePath, FileMode.Append)) { byte[] data = System.Text.Encoding.Default.GetBytes("Application_ThreadException:" + e.ToString()); efs.Write(data, 0, data.Length); } } } }