开发者

WPF: Changing config file user settings at runtime?

开发者 https://www.devze.com 2022-12-23 15:28 出处:网络
I\'m trying to change some config file user settings values in my WPF application, but its only working partly. The value is changed correctly and the program runs fine with the value. I can even rest

I'm trying to change some config file user settings values in my WPF application, but its only working partly. The value is changed correctly and the program runs fine with the value. I can even restart the program and the value is still the one i changed it to. The problem is that when i open the .exe.config file the value is still the old value. Im using this code to change the value:

Properties.Settings.Default.ProjectNumber = varTestExample;
Properties.Settings.Default.Save();

Where does this save code save the changes and how/where does the program read the value after i have run this code? If i run a clean version of the program the ProjectNumber valu开发者_如何学运维e is correctly taken from the .exe.config file and if i change the value in the config file it is also change when i run the program. But as soon as i run the above code the program is not reading the value from the config file. Why?


Settings are saved on a per-user basis. You should look into the Application Data folder in C:\Documents and Settings\[UserName]\... (WinXP) or in C:\Users\... (Vista/7).

Without saving any settings, the program uses the default configuration which is your *.exe.config file. But as soon as you save the changes, a user-specific settings file is created, and it loads this file at the next startup. I think, this should explain your behavior.


The Properties.Settings only refer to the user based setting, the Application Settings are a completely separate bunch of settings, which will get overwritten if you use ClickOnce Installs - so be carefull what configs you store there.

 private void updateDataInConfigFile()
    {
        Xml xmlConfigFile = new Xml(ProjectName.sSettingFileName);
        xmlConfigFile.SetValue("My Setting Section", "MyFirstSetting", MySettingValue);
}
  private void GetDataFromConfigFile()
    {
        Xml xmlConfigFile = new Xml(MyProject.sSettingFileName);

        txtAccessDriverId.Text = xmlConfigFile.GetValue("Mys Setting Section", "MyFirstSetting").ToString();
}


User saved settings are stored under User's AppData directory. Usually C:\Users\\AppData\Local\\

The values stored in the .exe.config file are the default values (that you set in the settings editor in VS)

Hope it helps

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号