/// <summary> /// 将进程序列化为xml /// </summary> /// <param name="path">文件保存路径</param> /// <param name="examPaper">进程类实例</param> public static void SetWhite(string path, WhiteProcessList processlst) { if (processlst == null) { throw new Exception("Parameter ExaminationPaper is null!"); } FileStream fs = null; try { XmlSerializer xs = new XmlSerializer(typeof(WhiteProcessList)); fs = new FileStream(path, FileMode.Create, FileAccess.Write); long fileLength = fs.Length;//获取流的长度 xs.Serialize(fs, processlst); fs.Close(); } catch { if (fs != null) { fs.Close(); } throw new Exception("Xml serialization failed!"); } }
/// <summary> /// 通过文件流反序列化试卷 /// </summary> /// <param name="path">文件路径</param> /// <returns>进程类</returns> public static WhiteProcessList GetWhite(String path) { if (whiteProcessList == null) { FileStream fs = null; try { XmlSerializer xs = new XmlSerializer(typeof(WhiteProcessList)); fs = new FileStream(path, FileMode.Open, FileAccess.Read); whiteProcessList = (WhiteProcessList)xs.Deserialize(fs); fs.Close(); return(whiteProcessList); } catch { if (fs != null) { fs.Close(); } throw new Exception("Xml deserialization failed!"); } } else { return(whiteProcessList); } }
//窗体加载事件 private void WhiteList_Load(object sender, EventArgs e) { //清空listBox listBox.Items.Clear(); //反序列化xml,获取内容 processList = XmlControl.GetWhite(GlobalConstants.WHITE_LIST_PATH); //获取白名单进程列表,增加到listBox中 foreach (MyProcess p in processList.processes) { listBox.Items.Add(p.processName); } }
private void MainForm_Load(object sender, EventArgs e) { whiteProcessList = XmlControl.GetWhite(GlobalConstants.WHITE_LIST_PATH); GetAllProcess(); }