StartProcess 使用 Properties.Settings 中的值导致异常结果

发布于 2024-08-25 02:07:01 字数 1421 浏览 6 评论 0原文

当尝试使用 Properties.Settings 中存储的应用程序名称启动应用程序时,我看到奇怪的行为。如果我在使用前没有重新设置该值(设置为相同值),则启动的应用程序将无法获取其应用程序设置的正确位置。也许显示代码会澄清我所说的内容。

这是启动新进程的代码。非常简单的东西。

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

我有另一个函数,它可以通过在标准 OpenFileDialog 中浏览文件来简单地设置设置。我不会费心在这里展示这一点,除了代码片段:

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

失败的代码(我无法控制)类似于:

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

由于 AppSettings 值总是返回“.”,因此 Path.GetFullPath 调用返回当前目录。如果我不重新设置Properties.Setting值,它就是启动应用程序的程序的路径;如果我重新设置设置,它就是已启动的应用程序的路径。

有什么想法吗?

谢谢, 重量

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光病人 2024-09-01 02:07:02

设置保存在安装的上下文中。如果您正在调试或以其他方式在 Visual Studio 之外运行此设置,则每次都会使用默认值,并且当您保存设置时,它只会在调试会话期间保留。

换句话说,查看 app.config 文件中的该设置。调试并更改值。再次查看 app.config 文件。它不会更新。另一方面,如果您部署此应用程序,app.config 将被更新(但请注意,如果您重新部署或重新安装,默认情况下,保存的设置将再次被覆盖)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文