static void Main() { try { //添加事件处理程序未捕获的异常 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //添加事件处理UI线程异常 Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); //添加事件处理非UI线程异常 AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("zh-Hans"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); bool isAppRunning = false; Mutex mutex = new Mutex(true, System.Diagnostics.Process.GetCurrentProcess().ProcessName, out isAppRunning); if (!isAppRunning) { DevExpress.XtraEditors.XtraMessageBox.Show("程序已运行,不能再次打开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); Environment.Exit(1); } BonusSkins.Register(); Application.Run(new MainForm()); } catch (Exception ex) { string str = ""; string strDateInfo = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n" + "出现应用程序未处理的异常:\r\n"; if (ex != null) { str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n", ex.GetType().Name, ex.Message, ex.StackTrace); } else { str = string.Format("应用程序线程错误:{0}", ex); } //写日志 WriteLog.WriteErrLog(str); DevExpress.XtraEditors.XtraMessageBox.Show("程序发生错误,请及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// 处理UI异常 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { string str = ""; Exception error = e.ExceptionObject as Exception; string strDateInfo = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n" + "出现应用程序未处理的异常:\r\n"; if (error != null) { str = string.Format(strDateInfo + "Application UnhandledException:{0};\n\r堆栈信息:{1}", error.Message, error.StackTrace); } else { str = string.Format("Application UnhandledError:{0}", e); } //写日志 WriteLog.WriteErrLog(str); DevExpress.XtraEditors.XtraMessageBox.Show("程序发生错误,请及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); }
/// <summary> /// 程序异常处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { string str = ""; string strDateInfo = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n" + "出现应用程序未处理的异常:\r\n"; Exception error = e.Exception as Exception; if (error != null) { str = string.Format(strDateInfo + "异常类型:{0}\r\n异常消息:{1}\r\n异常信息:{2}\r\n", error.GetType().Name, error.Message, error.StackTrace); } else { str = string.Format("应用程序线程错误:{0}", e); } //写日志 WriteLog.WriteErrLog(str); DevExpress.XtraEditors.XtraMessageBox.Show("程序发生错误,请及时联系作者!", "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error); }
/// <summary> /// 自动阅知 /// </summary> /// <param name="resPath"></param> /// <param name="contents"></param> public void Backup() { try { string s = ""; string t = ""; if (DateTime.Now.Month.ToString().Length == 1) { s = "0";//补0 } if (DateTime.Now.Day.ToString().Length == 1) { t = "0";//补0 } string date = DateTime.Now.Year.ToString().Substring(2) + s + DateTime.Now.Month.ToString() + t + DateTime.Now.Day.ToString(); string dirPath = HttpRuntime.AppDomainAppPath + "Log\\AutoBackup\\"; string fileName = "Log" + date + ".txt"; if (!File.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } B_OldToNew backup = new B_OldToNew(); int i = backup.DataTranslate(); if (i > 0) { string contents = "执行自动迁移旧数据 " + DateTime.Now.ToString() + "\r\n"; File.AppendAllText(dirPath + fileName, contents, Encoding.UTF8); } } catch (Exception ex) { WriteLog writelog = new WriteLog(); writelog.WriteErrLog("AutoBackup", DateTime.Now.ToString() + ex.ToString()); throw ex; } }
/// <summary> /// 自动阅知 /// </summary> /// <param name="resPath"></param> /// <param name="contents"></param> public void Backup() { try { string s = ""; string t = ""; if (DateTime.Now.Month.ToString().Length == 1) { s = "0";//补0 } if (DateTime.Now.Day.ToString().Length == 1) { t = "0";//补0 } string date = DateTime.Now.Year.ToString().Substring(2) + s + DateTime.Now.Month.ToString() + t + DateTime.Now.Day.ToString(); string dirPath = HttpRuntime.AppDomainAppPath + "Log\\AutoBackup\\"; string fileName = "Log" + date + ".txt"; if (!File.Exists(dirPath)) { Directory.CreateDirectory(dirPath); } B_OldToNew backup = new B_OldToNew(); int i=backup.DataTranslate(); if (i > 0) { string contents = "执行自动迁移旧数据 " + DateTime.Now.ToString() + "\r\n"; File.AppendAllText(dirPath + fileName, contents, Encoding.UTF8); } } catch (Exception ex) { WriteLog writelog = new WriteLog(); writelog.WriteErrLog("AutoBackup",DateTime.Now.ToString()+ex.ToString()); throw ex; } }