Question

I have a config file for my program as follows

<appSettings>
    <add key="ShowDebugWindow" value="false"/>
    <add key="AppDataSubFolder" value="DLI\Keymon"/>
    <add key="KeymonSettingsFile" value="Keymon.xml"/>
    <add key="KeymonSettingsFileExtension" value="XML"/>
</appSettings>

With some digging around I found out how I can A) check if that key exists B) add a new one if needed. Since my program is in Program Files I found out that to avoid having the config file saved to virtual store that I have to have my program run as admin. That is not allowable so I decided to make a console project that would fix this for me. Easy enough.

So i did this

    public static int Main(string[] args)
    {
        FixConfigFile();
        return 0;
    }
    private static void FixConfigFile()
    {//warning if the program is writing to a protect area, the config file might be redirected to virtual store
        UpdateSettingIfMissing("ShowDebugWindow", "false");
        UpdateSettingIfMissing("AppDataSubFolder", "DLI\\Keymon");
        UpdateSettingIfMissing("KeymonSettingsFile", "Keymon.xml");
        UpdateSettingIfMissing("KeymonSettingsFileExtension", "XML");
        UpdateSettingIfMissing("LeftButton1", "");
        UpdateSettingIfMissing("RightButton1", "");
        UpdateSettingIfMissing("FunctionButton1", "");
        UpdateSettingIfMissing("FunctionButton2", "");
        UpdateSettingIfMissing("FunctionButton3", "");
        UpdateSettingIfMissing("FunctionButton4", "");
        UpdateSettingIfMissing("FunctionButton5", "");
        UpdateSettingIfMissing("FunctionButton6", "");
    }
    private static void UpdateSettingIfMissing(string key, string defaultValue)
    {
        var setting = ConfigurationManager.AppSettings[key];
        if (setting == null)
        {
            UpdateSetting(key, defaultValue);
        }
    }
    private static void UpdateSetting(string key, string value)
    {
        Configuration configuration = ConfigurationManager.OpenExeConfiguration(@"Keymon.exe");
        configuration.AppSettings.Settings.Add(key, value);
        configuration.Save(ConfigurationSaveMode.Minimal);
    }

The config file then would look like this

    <add key="ShowDebugWindow" value="false,false" />
    <add key="AppDataSubFolder" value="DLI\Keymon" />
    <add key="KeymonSettingsFile" value="Keymon.xml,Keymon.xml" />
    <add key="KeymonSettingsFileExtension" value="XML,XML" />
    <add key="LeftButton1" value="" />
    <add key="RightButton1" value="" />
    <add key="FunctionButton1" value="" />
    <add key="FunctionButton2" value="" />
    <add key="FunctionButton3" value="" />
    <add key="FunctionButton4" value="" />
    <add key="FunctionButton5" value="" />
    <add key="FunctionButton6" value="" />

notice that the value now has "2" values that are comma separated... WHY??? So as a test I deleted all keys, and ran my program. The values are all correct now, but now keymon crashes because the key/values were not loaded. Which is strange because in keymon i run this..

        if (RunFixer)
        {
            System.Diagnostics.Process.Start("KeymonConfigFixer.exe");
            ConfigurationManager.RefreshSection("appSettings");
        }

So i don't get it. What am I missing? why is it doing this? Help

Was it helpful?

Solution

It's because in UpdateSettingIfMissing you use ConfigurationManager.AppSettings[key] which opens the app settings for the current application (your console app) while in UpdateSetting you open the settings for Keymon.exe

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top