/// <summary> /// 使用指定的 System.IO.Stream 序列化指定的 System.Object 并将 XML 文档写入文件。 /// </summary> /// <param name="type"></param> /// <param name="obj"></param> /// <returns></returns> public static string Serializer(Type type, object obj) { MemoryStream stream = new MemoryStream(); XmlSerializer serializer = new XmlSerializer(type); try { serializer.Serialize((Stream)stream, obj); } catch (Exception ex) { ToolLogs.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "序列化", ex); } stream.Position = 0L; StreamReader reader = new StreamReader(stream); return(reader.ReadToEnd()); }
/// <summary> /// 打开导入的文件 /// </summary> public static void OpenImputFileTask() { Task t1 = new Task(() => { do { Thread.Sleep(1000); try { OpenImputFIle(); } catch (Exception e) { ToolLogs.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "自动打开文件", e); } } while (!SystemCommon.IsExitSystem); }); t1.Start(); }
/// <summary> /// 反序列化 /// </summary> /// <param name="path"></param> /// <returns></returns> public static T Deserialize <T>(string path) { if (!File.Exists(path)) { return(default(T)); } try { Stream steam = File.Open(path, FileMode.Open); BinaryFormatter bf = new BinaryFormatter(); object o = bf.Deserialize(steam); steam.Close(); return((T)o); } catch (Exception ex) { ToolLogs.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "反序列化", ex); return(default(T)); } }
public App() { #region 序退出异常处理 var pro = Process.GetCurrentProcess(); pro.EnableRaisingEvents = true; pro.Exited += (sender, e1) => { ToolLogs.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "在进程退出时发生"); }; AppDomain.CurrentDomain.ProcessExit += (sender, e1) => { ////晚于this.Exit //System.Threading.Monitor.TryEnter(obj, 500); ////后来改为 //System.Threading.Monitor.Enter(obj); ToolLogs.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "当默认应用程序域的父进程存在时发生"); ////不能在这保存所有便签设置,因为这时所有的程序已经全部关闭。 //NotepadManage.SaveSetings(); }; //异常处理 this.DispatcherUnhandledException += (sender, e1) => { ToolLogs.Debug(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "系统异常处理", e1.Exception); e1.Handled = true;//使用这一行代码告诉运行时,该异常被处理了,不再作为UnhandledException抛出了。 SystemCommon.IsExitSystem = true; MessageBox.Show("我们很抱歉,当前应用程序遇到一些问题,该操作已经终止,将要重新启动。" + e1.Exception.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error);//这里通常需要给用户一些较为友好的提示,并且后续可能的操作 //保存所有便签设置 NotepadManage.SaveSetings(); //不要在这打开程序,如果你的程序是刚打开时出现错误,用户将无法关闭。这将是用户的恶梦。 SystemCommon.ExitSystem(); //System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location); }; this.Exit += (sender, e1) => { //早于AppDomain.CurrentDomain.ProcessExit ToolLogs.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "在进程退出时发生Exit"); ////不能在这保存所有便签设置,因为这时所有的程序已经全部关闭。 // NotepadManage.SaveSetings(); //e1.ApplicationExitCode }; #endregion bool result = ImputCacheFile(); //非调试模式 if (!System.Diagnostics.Debugger.IsAttached) { Client.Pub.ServiceProxyFactoryLib.ProxyBinding.ReplaceAddress = true; Client.Pub.ServiceProxyFactoryLib.ProxyBinding.ReplacePort = true; //检测是否已有一个程序实例运行 this.Startup += new StartupEventHandler(App_Startup); //下载版本不同的文件 string loadPath = Path.Combine(SystemCommon.Path, "AutoUpdate"); //复制自动更新程序文件 Tools.FileManage.CopyDir(Path.Combine(loadPath, "LoadDown\\AutoUpdate\\"), loadPath); Tools.FileManage.IsUpdateThread(); //绑定扩展名 Task task = new Task(() => { try { BindingExtend(); } catch (Exception e) { ToolLogs.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "绑定扩展名", e); } }); task.Start(); } }