HI All,
After a lot of research, I am now able to write to the <ApplicationSettings> section of the .config file/settings per the code below however, now I have a new problem. When I run the program from the Program Files Directory, UAC is preventing me from being able to overwrite/save the changes.
I know UAC exists for a good reason but, It's preventing my program from being able to edit it's own settings. I am using Microsoft's recommended approach for storing application settings so there must be a way to allow the program to save it's own data. I know I could run the program as admin or even use compatibility mode to run as admin but that just seems jenky! The last time I installed iTunes and changed the program configuration, I didn't have to run as admin so I really shouldn't have to do the same when I'm using Microsoft's own technology to build a program. How can I solve this problem? I am simply trying to allow my application.exe file to write to the application.exe.config file. It really seems like this should be doable without admin rights or turning off UAC. Thanks
Private Sub WriteApplicationSetting(strSetting As String, strvalue As String)
Dim config As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Dim applicationSectionGroup As ConfigurationSectionGroup = config.GetSectionGroup("applicationSettings")
Dim strAppName As String = System.Reflection.Assembly.GetExecutingAssembly.GetName().Name
Dim applicationConfigSection As ConfigurationSection = applicationSectionGroup.Sections(strAppName & ".My.MySettings")
Dim clientSection As ClientSettingsSection = CType(applicationConfigSection, ClientSettingsSection)
' set a value to that specific property
Dim applicationSetting As SettingElement = clientSection.Settings.Get(strSetting)
applicationSetting.Value.ValueXml.InnerText = strvalue
' without this, saving won't work
applicationConfigSection.SectionInformation.ForceSave = True
' save
config.Save()
End Sub