I created a c#.net application which uses a App.Config.
While debugging everything works fine of course.
I can access the file to read and write.
But when i created a installer for my application the writing part didn’t work anymore.
This was because of the fact that the application was installed in program files. and you can’t write to de program files folder…
I kinda thought of a solution to place the app.config in de AppData Folder but I cant get it to work.
What i want to know is how to instruct my installer to place a file in the correct location and how to create the correct function to write it it on that location.
Here is what i had so far:
public static void WriteValue(string key, string value)
{
string configPath = System.Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData) +
"\\Application\\app.config";
ExeConfigurationFileMap map = new ExeConfigurationFileMap(configPath);
// Open App.Config of executable
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map,
ConfigurationUserLevel.None);
// Add an Application Setting.
config.AppSettings.Settings.Remove(key);
config.AppSettings.Settings.Add(key, value);
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified);
// Force a reload of a changed section.
ConfigurationManager.RefreshSection("appSettings");
}
Thank you guys in Advance
The file
App.configis the name used during the development process and during debugging the VS reads it just fine.But when you run your executable
YourProgram.exeoutside the studio, it searches for file namedYourProgram.exe.configHope it helps.