Search code examples
visual-studio-2017configurationmanager

ConfigurationManager issue in VS 2017


The following code, when run in Visual Studio 2015 and earlier, results in a message box being shown with the expected value "12345".

    string executablePath = Application.ExecutablePath;
    executablePath = Path.GetFileNameWithoutExtension(executablePath);
    executablePath = executablePath + ".vshost.exe";

    if (!File.Exists(executablePath))
        throw new FileNotFoundException(executablePath);

    Configuration cfg = ConfigurationManager.OpenExeConfiguration(executablePath);
    cfg.AppSettings.Settings.Add("Testing", "12345");            
    cfg.Save(ConfigurationSaveMode.Modified);
    ConfigurationManager.RefreshSection(cfg.AppSettings.SectionInformation.Name);

    string testing = ConfigurationManager.AppSettings["Testing"];

    MessageBox.Show(testing);

When I run the same code in Visual Studio 2017 the message box displays a blank value.

Is this a bug in Visual Studio 2017 or does the code require modification?

UPDATE (specific cause):

So the main cause, along with the accepted answer, was that I had opened the solution in VS 2015 which generated the *.vshost.exe related files. Later I opened the solution in VS 2017 and, of course, the *.vshost.exe files aren't cleaned automatically so were still there.

UPDATE 2 (for those who want to be able to use similar code in both):

string executablePath = Application.ExecutablePath;
executablePath = Path.GetFileNameWithoutExtension(executablePath);
executablePath = executablePath + ".vshost.exe";

//  Check if the *.vshost.exe exists
if (File.Exists(executablePath))
{                    
    try
    {
        //  If deleting throws an exception then the stub is being run by *.vshost.exe while 
        //      debugging which means this is NOT Visual Studio 2017 (*.vshost.exe is no longer used in VS 2017)
        File.Delete(executablePath);
        //  If it deletes then use the regular app path since VS2017 is using that now.
        executablePath = Application.ExecutablePath;
    }
    catch (Exception)
    {                        
        executablePath = Application.ExecutablePath;
    }
}
else                
    executablePath = Application.ExecutablePath; 

Configuration cfg = ConfigurationManager.OpenExeConfiguration(executablePath);
cfg.AppSettings.Settings.Add("Testing", "12345");            
cfg.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection(cfg.AppSettings.SectionInformation.Name);

string testing = ConfigurationManager.AppSettings["Testing"];

MessageBox.Show(testing);

Solution

  • The debugger hosting process has been removed in VS2017, so when you run your application the path you give to the OpenExeConfiguration method is incorrect.

    Instead, pass Application.ExecutablePath to that method and it should work. The same should work in VS 2015 if you turn off the hosting process (Project properties -> Debug -> Enable the Visual Studio hosting process).

    It's odd that you were using the hosting process path in the first place, as that would only ever work while running in the debugger from within Visual Studio.