Assembly configuration file (Program.config) created under administrator account. When I switch to user account (not administrator), I cannot write configuration开发者_如何学Python section to this file. Specifically, code looks like this:
//creating section (and file) with administrator access
UserInfoConfigurationSection user_conf = new UserInfoConfigurationSection();
Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
conf.Sections.Add("userInfo", user_conf);
conf.Save(ConfigurationSaveMode.Full);
//trying to write to section under user account
Configuration conf = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
UserInfoConfigurationSection user_conf = (UserInfoConfigurationSection)conf.Sections["userInfo"];
user_conf.SerialKey = textBoxSerial.Text;
user_conf.UserCompany = textBoxCompany.Text;
user_conf.UserName = textBoxUser.Text;
conf.Save(ConfigurationSaveMode.Full);//getting exception
What is the best option to write and read settings under any account?
Use the right tool for the job and create the settings you want to be changed at runtime with the user scope: http://msdn.microsoft.com/en-us/library/aa730869%28v=vs.80%29.aspx#settingscs_topic2
精彩评论