/// <summary> /// 環境設定をファイルから読み込む /// </summary> /// <param name="fileName">読み込む環境設定ファイル名</param> /// <returns>読み込んだ環境設定情報</returns> public static EnvOption LoadFile(string fileName) { //プロジェクトファイルを読み込み自分自身にコピーする //XmlSerializer serializer = new XmlSerializer(typeof(EnvOption)); //FileStream fs; //EnvOption option = null; //using (fs = new FileStream(fileName, FileMode.Open)) //{ // option = (EnvOption)serializer.Deserialize(fs); //} //return option; EnvOption option = null; try { option = loadFromXml(fileName); } catch (Exception err) { util.Msgbox.Error("環境設定ファイルが読み込めませんでした\n" + err.Message); option = new EnvOption(); //デフォルト値をセットする } return(option); }
/// <summary> /// 環境設定をファイルに保存する /// </summary> /// <param name="fileName">作成する環境設定ファイル名</param> /// <param name="option">保存する環境設定</param> public static void SaveFile(string fileName, EnvOption option) { //環境設定をファイルに保存する //XmlSerializer serializer = new XmlSerializer(typeof(EnvOption)); //FileStream fs; //using (fs = new FileStream(fileName, FileMode.Create)) //{ // serializer.Serialize(fs, option); //} try { saveToXml(fileName, option); } catch (Exception err) { util.Msgbox.Error("環境設定ファイルの保存に失敗しました\n" + err.Message); } }
private static EnvOption loadFromXml(string filePath) { EnvOption option = new EnvOption(); FileStream fs; using (fs = new FileStream(filePath, FileMode.Open)) { XmlDocument doc = new XmlDocument(); doc.Load(fs); //ルートノードを読み込む XmlNodeList nodeList = doc.GetElementsByTagName("EnvOption"); if (nodeList == null || nodeList.Count == 0) { throw new ApplicationException("環境設定ファイル読み込みエラー\nこのファイルは不正な設定ファイルです"); } //設定値を読み込む foreach (XmlElement element in nodeList[0].ChildNodes) { switch (element.Name) { case "HistoryProject": option.HistoryProject = getHistoryTableFromXmlElement(element); break; case "HistoryFile": option.HistoryFile = getHistoryTableFromXmlElement(element); break; case "OpenedList": option.OpenedList = getOpenedListFromXmlElement(element); break; case "BookmarkList": option.BookmarkList = getBookmarkListFromXmlElement(element); break; case "SearchOption": option.SearchOption = getSearchOptionFromXmlElement(element); break; case "SearchResultShowDiaglog": option.SearchResultShowDiaglog = Boolean.Parse(element.InnerText); break; case "LastProjectPath": option.LastProjectPath = element.InnerText; break; case "ProjectOpenedLastProject": option.ProjectOpenedLastProject = Boolean.Parse(element.InnerText); break; case "ProjectOpenedLastFile": option.ProjectOpenedLastFile = Boolean.Parse(element.InnerText); break; case "ProjectWatchProjectPath": option.ProjectWatchProjectPath = Boolean.Parse(element.InnerText); break; case "ExecSaveAll": option.ExecSaveAll = Boolean.Parse(element.InnerText); break; case "ExecOpenLog": option.ExecOpenLog = Boolean.Parse(element.InnerText); break; case "ExecKrkrKill": option.ExecKrkrKill = Boolean.Parse(element.InnerText); break; case "ExecAddClearOption": option.ExecAddClearOption = Boolean.Parse(element.InnerText); break; case "CodeComplateSeepTime": option.CodeComplateSeepTime = Int32.Parse(element.InnerText); break; case "WindowStartPos": option.WindowStartPos = (FormStartPosition)Enum.Parse(typeof(FormStartPosition), element.InnerText); break; case "WindowState": option.WindowState = (FormWindowState)Enum.Parse(typeof(FormWindowState), element.InnerText); break; case "WindowSize": option.WindowSize = getSizeFromXmlElement(element); break; case "ToolBarFilePos": option.ToolBarFilePos = getToolBarPosFromXmlElement(element); break; case "ToolBarEditPos": option.ToolBarEditPos = getToolBarPosFromXmlElement(element); break; case "ToolBarSearchPos": option.ToolBarSearchPos = getToolBarPosFromXmlElement(element); break; case "ToolBarBookmarkPos": option.ToolBarBookmarkPos = getToolBarPosFromXmlElement(element); break; case "ToolBarExecPos": option.ToolBarExecPos = getToolBarPosFromXmlElement(element); break; case "ToolBarViewPos": option.ToolBarViewPos = getToolBarPosFromXmlElement(element); break; case "HelpHelpWindow": option.HelpHelpWindow = (HelpWindowState)Enum.Parse(typeof(HelpWindowState), element.InnerText); break; } } } return(option); }
/// <summary> /// 設定情報をXMLファイルで作成し保存する /// </summary> /// <param name="filePath"></param> /// <param name="option"></param> private static void saveToXml(string filePath, EnvOption option) { using (FileStream fs = new FileStream(filePath, FileMode.Create)) { using (XmlTextWriter xw = new XmlTextWriter(fs, Encoding.UTF8)) { xw.Formatting = Formatting.Indented; xw.WriteStartDocument(); xw.WriteStartElement("EnvOption"); xw.WriteStartElement("HistoryProject"); writeXmlHistoryTable(xw, option.HistoryProject); xw.WriteEndElement(); xw.WriteStartElement("HistoryFile"); writeXmlHistoryTable(xw, option.HistoryFile); xw.WriteEndElement(); xw.WriteStartElement("OpenedList"); writeXmlOpenedList(xw, option.OpenedList); xw.WriteEndElement(); xw.WriteStartElement("BookmarkList"); writeXmlBookmarkList(xw, option.BookmarkList); xw.WriteEndElement(); xw.WriteStartElement("SearchOption"); writeXmlSearchOption(xw, option.SearchOption); xw.WriteEndElement(); xw.WriteStartElement("SearchResultShowDiaglog"); xw.WriteValue(option.SearchResultShowDiaglog); xw.WriteEndElement(); xw.WriteStartElement("LastProjectPath"); xw.WriteValue(option.LastProjectPath); xw.WriteEndElement(); xw.WriteStartElement("ProjectOpenedLastProject"); xw.WriteValue(option.ProjectOpenedLastProject); xw.WriteEndElement(); xw.WriteStartElement("ProjectOpenedLastFile"); xw.WriteValue(option.ProjectOpenedLastFile); xw.WriteEndElement(); xw.WriteStartElement("ProjectWatchProjectPath"); xw.WriteValue(option.ProjectWatchProjectPath); xw.WriteEndElement(); xw.WriteStartElement("ExecSaveAll"); xw.WriteValue(option.ExecSaveAll); xw.WriteEndElement(); xw.WriteStartElement("ExecOpenLog"); xw.WriteValue(option.ExecOpenLog); xw.WriteEndElement(); xw.WriteStartElement("ExecKrkrKill"); xw.WriteValue(option.ExecKrkrKill); xw.WriteEndElement(); xw.WriteStartElement("ExecAddClearOption"); xw.WriteValue(option.ExecAddClearOption); xw.WriteEndElement(); xw.WriteStartElement("CodeComplateSeepTime"); xw.WriteValue(option.CodeComplateSeepTime); xw.WriteEndElement(); xw.WriteStartElement("WindowStartPos"); xw.WriteValue(option.WindowStartPos.ToString()); xw.WriteEndElement(); xw.WriteStartElement("WindowState"); xw.WriteValue(option.WindowState.ToString()); xw.WriteEndElement(); xw.WriteStartElement("WindowSize"); writeXmlSize(xw, option.WindowSize); xw.WriteEndElement(); xw.WriteStartElement("ToolBarFilePos"); writeXmlToolBarPos(xw, option.ToolBarFilePos); xw.WriteEndElement(); xw.WriteStartElement("ToolBarEditPos"); writeXmlToolBarPos(xw, option.ToolBarEditPos); xw.WriteEndElement(); xw.WriteStartElement("ToolBarSearchPos"); writeXmlToolBarPos(xw, option.ToolBarSearchPos); xw.WriteEndElement(); xw.WriteStartElement("ToolBarBookmarkPos"); writeXmlToolBarPos(xw, option.ToolBarBookmarkPos); xw.WriteEndElement(); xw.WriteStartElement("ToolBarExecPos"); writeXmlToolBarPos(xw, option.ToolBarExecPos); xw.WriteEndElement(); xw.WriteStartElement("ToolBarViewPos"); writeXmlToolBarPos(xw, option.ToolBarViewPos); xw.WriteEndElement(); writeElement(xw, "HelpHelpWindow", option.HelpHelpWindow); xw.WriteEndElement(); xw.WriteEndDocument(); } } }