开发者

StartProcess using value from Properties.Settings causes Unusual Results

开发者 https://www.devze.com 2022-12-22 19:15 出处:网络
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 app

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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号