Abedron
Abedron

Reputation: 771

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

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?

Upvotes: -1

Views: 205

Answers (2)

fred
fred

Reputation: 1

I found the user.config file in

C:\Users\
(user name)
\AppData\Local\
(program Author Name or Company)
\(Application / Program Name)_Url_
(some gobbily gook)
\Version Number

Example

C:\Users\fred\AppData\Local\Hewlett-Packard_Company\FileReader.exe_Url_gygnbk1oi1wo32yb1eciccg4qkvfbjhl\1.0.0.0

Upvotes: 0

IceCode
IceCode

Reputation: 1751

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");  
    }  
}

Upvotes: 0

Related Questions