public static SettingModel ReadSetting() { SettingModel model = new SettingModel(); //从解决方案目录下面读取配置信息 if (!Directory.Exists(configDirectory)) { Directory.CreateDirectory(configDirectory); } if (File.Exists(configDirectory + "\\" + configName)) { using (StreamReader sr = new StreamReader(configDirectory + "\\" + configName)) { string xmlString = sr.ReadToEnd(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(SettingModel)); using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString))) { using (XmlReader xmlReader = XmlReader.Create(xmlStream)) { Object obj = xmlSerializer.Deserialize(xmlReader); model = (SettingModel)obj; } } } } return model; }
public static void SaveSetting(SettingModel obj) { string xmlString; XmlSerializer xmlSerializer = new XmlSerializer(typeof(SettingModel)); using (MemoryStream ms = new MemoryStream()) { xmlSerializer.Serialize(ms, obj); xmlString = Encoding.UTF8.GetString(ms.ToArray()); } StreamWriter sw = new StreamWriter(configDirectory + "\\" + configName, false); sw.Write(xmlString); sw.Close(); //重置所有的配置(null时会重新从配置文件中读取) BaseUCSetting.SettingModel = null; if (ThemeSetting.ThemeChanged) { if (MessageBox.Show("Program has deteched that you have changed your theme, which need restart visual studio to make it work, do you want restart visual studio now?", "Restart Visual Studio", MessageBoxButtons.OKCancel) == DialogResult.OK) { VSBase.ReStartVS(); } } }