I have been unable to find any other way to update a user config file than the following method.
public class SettingServices : ISettingServices
{
public UserSetting GetUserSetting()
{
XmlSerializer reader = new XmlSerializer(typeof(UserSetting));
StreamReader file = new StreamReader(UserConfig);
UserSetting settings = new UserSetting();
settings = (UserSetting)reader.Deserialize(file);
return settings;
}
public void CreateUserSetting(UserSetting userSetting)
{
XmlSerializer writer = new XmlSerializer(typeof(UserSetting));
StreamWriter file = new StreamWriter(UserConfig);
writer.Serialize(file, userSetting);
file.Close();
}
public void UpdateUserSetting(UserSetting userSetting)
{
UserSetting currentSettings = GetUserSetting();
UserSetting newSetting = new UserSetting();
newSetting.Name = (userSetting.Name == null) ? currentSettings.Name : userSetting.Name;
newSetting.ViewChangeLog = (userSetting.ViewChangeLog == null) ? currentSettings.ViewChangeLog : userSetting.ViewChangeLog;
newSetting.PrimaryRowColor = (userSetting.PrimaryRowColor == null) ? currentSettings.PrimaryRowColor : userSetting.PrimaryRowColor;
newSetting.SecondaryRowColor = (userSetting.SecondaryRowColor == null) ? currentSettings.SecondaryRowColor : userSetting.SecondaryRowColor;
newSetting.Font = (userSetting.Font == null) ? currentSettings.Font : userSetting.Font;
CreateUserSetting(newSetting);
}
private static string UserConfig
{
get
{
string configDirectory = new Uri(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\CVS\\ICMQuery").LocalPath;
return new Uri(configDirectory + "\\" + System.Environment.UserName + ".config").LocalPath;
}
}
}
Can someone please show me the light. I would preferably like to be able to update singular elements within the UserSetting
class without destroying and rewriting the file every time I have to update it.
PrimaryRowColor
(One of the alternating row colors forDataGridViews
) setting from red to blue. Preferably I'd like to just be able to update that node in the XML file without having to rewrite the whole file every time a change is made. \$\endgroup\$ – aaronmallen Jul 9 '14 at 13:50