views:

78

answers:

1

Hello All,

I am seeing odd behavior when trying to launch an application using an application name stored in Properties.Settings. If I don't re-set the value (to the same value) before using it, the launched application will fail to get the correct location for its application settings. Perhaps showing the code will clear up what I'm saying.

Here is the code that starts the new process. Pretty straight-forward stuff.

    private void StartNewApplication()
    {
        Process mainAppProcess = new Process();
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = Properties.Settings.Default.TheApplicationPath;
        startInfo.WindowStyle = ProcessWindowStyle.Normal;

        mainAppProcess.StartInfo = startInfo;
        mainAppProcess.Start();
        mainAppProcess.WaitForExit();
    }

I have another function that simply sets the Setting by browsing for the file in a standard OpenFileDialog. I won't bother showing that here, except for the snippet:

        if (fileDialog.ShowDialog().Value == true)
        {
            Properties.Settings.Default.TheApplicationPath = fileDialog.FileName;
            Properties.Settings.Default.Save();
        }

The code that was failing (which I have no control over) is something like:

    private static string GetConfigFolder()
    {
        string configFolder = ConfigurationManager.AppSettings.Get("ConfigFolder");
        configFolder = Path.GetFullPath(configFolder);           
        return string.IsNullOrEmpty(configFolder) ? Environment.CurrentDirectory : configFolder;
    }

Since the AppSettings value is always coming back ".", the Path.GetFullPath call returns the current directory. If I don't re-set the Properties.Setting value, it is the path of the program that starts the application; if I re-set the Setting, it is the path of the application that has been launched.

Any ideas?

Thanks, WtS

+1  A: 

The settings are saved within the context of an installation. If you are debugging or otherwise running this out of Visual Studio, the default value is going to be used every time, and when you save the setting, it is only going to stick for the duration of your debugging session.

Put another way, look at that setting in your app.config file. Debug and change the value. Look again at the app.config file. It does not get updated. If you deploy this application, on the other hand, app.config would be updated (note, however, that it you redeploy or reinstall, the saved settings will again be overwritten, by default).

Jay
Understood.However, the issue here is that the default setting is exactly the same as the "updated" setting. I browse to and select the same path as is saved in the default setting.
Wonko the Sane
I had something similar happen, but can't remember the specifics. Have a look at the file generated for the application settings, and make sure there aren't unexpected default values in the attributes. My issue had something to do with the settings designer and the codebehind being out of sync.
Jay
I went so far as to run it both before and after re-setting the value, and doing a String.Compare in the Watch window, with a result of 0.
Wonko the Sane