using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; namespace VersionUpdate { static class Program { internal static string ExeName { get; private set; } /// /// 应用程序的主入口点。 /// [STAThread] static void Main(string[] param) { try { #region 系统捕获异常 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic); Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException); AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException); SetAllowUnsafeHeaderParsing20(true); #endregion Client client = new Client(); if (param.Length > 0) { ExeName = Client.MapFile(param[0]); } if (client.CheckInstall()) { if (param.Length > 0) { Client.StartProcess(param[0]); return; } } else { if (client.CheckVersion()) { if (client.CheckInstall()) { if (param.Length > 0) { Client.StartProcess(param[0]); return; } } } else if (client.GetVersions().Count == 0 && param.Length == 0) MessageBox.Show("没有发现新版本!", "友情提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } catch (Exception ex) { } } #region 系统异常捕获 static bool SetAllowUnsafeHeaderParsing20(bool useUnsafe) { try { //Get the assembly that contains the internal class System.Reflection.Assembly aNetAssembly = System.Reflection.Assembly.GetAssembly(typeof(System.Net.Configuration.SettingsSection)); if (aNetAssembly != null) { //Use the assembly in order to get the internal type for the internal class Type aSettingsType = aNetAssembly.GetType("System.Net.Configuration.SettingsSectionInternal"); if (aSettingsType != null) { //Use the internal static property to get an instance of the internal settings class. //If the static instance isn't created allready the property will create it for us. object anInstance = aSettingsType.InvokeMember("Section", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.NonPublic, null, null, new object[] { }); if (anInstance != null) { //Locate the private bool field that tells the framework is unsafe header parsing should be allowed or not System.Reflection.FieldInfo aUseUnsafeHeaderParsing = aSettingsType.GetField("useUnsafeHeaderParsing", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance); if (aUseUnsafeHeaderParsing != null) { aUseUnsafeHeaderParsing.SetValue(anInstance, useUnsafe); return true; } } } } return false; } catch (Exception ex) { //MessageBox.Show(ex.Message + " - " + ex.StackTrace); } return false; } static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e) { try { string str = GetExceptionMsg(e.Exception, e.ToString()); //MessageBox.Show(str, "子线程错误", MessageBoxButtons.OK, MessageBoxIcon.Error); throw new Exception(str); } catch (Exception ex) { //MessageBox.Show(ex.Message, "子线程错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { try { string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString()); // MessageBox.Show(str, "主线程错误", MessageBoxButtons.OK, MessageBoxIcon.Error); throw new Exception(str); } catch (Exception ex) { //MessageBox.Show(ex.Message, "主线程错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } } static string GetExceptionMsg(Exception ex, string backStr) { StringBuilder sb = new StringBuilder(); sb.AppendLine("****************************异常文本****************************"); sb.AppendLine("【出现时间】:" + DateTime.Now.ToString()); if (ex != null) { sb.AppendLine("【异常类型】:" + ex.GetType().Name); sb.AppendLine("【异常信息】:" + ex.Message); sb.AppendLine("【堆栈调用】:" + ex.StackTrace); } else { sb.AppendLine("【未处理异常】:" + backStr); } sb.AppendLine("***************************************************************"); return sb.ToString(); } /// /// 退出 /// public static void Exit(bool start = false) { try { if (start) { //程序位置 string strAppFileName = Process.GetCurrentProcess().MainModule.FileName; Process myNewProcess = new Process(); //要启动的应用程序 myNewProcess.StartInfo.FileName = strAppFileName; // 设置要启动的进程的初始目录 myNewProcess.StartInfo.WorkingDirectory = Application.ExecutablePath; //启动程序 myNewProcess.Start(); } //结束该程序 // Application.Exit(); //结束该所有线程 //Environment.Exit(0); Process.GetCurrentProcess().Kill(); } catch (Exception) { } } #endregion } }