How modify user.config from AppData folder with another application?

76 Views Asked by At

I want to edit the user.config file from the installer or a test that is not part of the application with the config I want to edit. Therefore I need a path for user.config. Path to user.config is like this: c:\Users\SomeUser\AppData\Local\SomeApp\SomeAppName.exe_StrongName_2ndz3ofoffkd0gg0sv1qxyrthr3yi236\2.4.2.0\.

With ConfigurationManager.OpenExeConfiguration(...) I get just *.exe.config path from installation folder and not AppData.

The second option is to fold the path. I can get everything except the correct StrongName hash 2ndz3ofoffkd0gg0sv1qxyrthr3yi236 which does not match the StrongName (PublicKeyToken) from the assembly.

Can you advise me to programmatically change user.config?

1

There are 1 best solutions below

3
IceCode On

The following method can potentially help you:

public void UpdateUserSettings(string key, string value)  
{  
    try  
    {  
        System.Configuration.ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();  
        fileMap.ExeConfigFilename = AppDomain.CurrentDomain.BaseDirectory + "..\\..\\user.config"; 
        Configuration configFile = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

        var settings = configFile.AppSettings.Settings;  
        if (settings[key] == null)  
        {  
            settings.Add(key, value);  
        }  
        else  
        {  
            settings[key].Value = value;  
        }

        configFile.Save(ConfigurationSaveMode.Modified);  
        ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);  
    }  
    catch (Exception e)  
    {  
        Console.WriteLine("Error writing user settings");  
    }  
}