从当前文件夹启动 .exe 有时会失败

发布于 2024-08-24 20:36:33 字数 350 浏览 3 评论 0 原文

我有一个应用程序启动一个可执行文件,该可执行文件与该应用程序位于同一文件夹中,通过执行以下操作:

            Process procStarter = new Process();
            procStarter.StartInfo.FileName = "OtherApp.exe";
            procStart.Start();

效果很好,直到我在应用程序中使用文件打开或文件保存对话框。然后就找不到OtherApp.exe了。

这看起来正常吗?我可以通过将当前文件夹添加到 StartInfo.Filename 来修复它吗(以及如何获取当前文件夹)?

I have an app launching an executable which is in the same folder as that app, by doing:

            Process procStarter = new Process();
            procStarter.StartInfo.FileName = "OtherApp.exe";
            procStart.Start();

which works fine, until I used a file open or file save dialog in my app. Then it can't find OtherApp.exe.

Does that seem normal? Can I just fix it by adding the current folder to StartInfo.Filename (and how do I obtain the current folder)?

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

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

发布评论

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

评论(6

嘴硬脾气大 2024-08-31 20:36:34

使用文件对话框可能会更改进程的当前目录。要访问与当前可执行文件位于同一文件夹中的文件,可以使用以下代码:

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
path = Path.Combine(path, "OtherApp.exe");

Using the file dialog probably changes the current directory of your process. To access a file in the same folder as your current executable you can use the following code:

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
path = Path.Combine(path, "OtherApp.exe");
羅雙樹 2024-08-31 20:36:34

或者您可以在代码中添加:

saveFileDialog1.RestoreDirectory = true ;

提示输入文件名时。

Or you could add to your code:

saveFileDialog1.RestoreDirectory = true ;

when prompting for filenames.

在风中等你 2024-08-31 20:36:34

问题是您在执行其他文件操作时可能会更改当前工作目录。

您想要记住其他海报向您展示的路径,但您可能想要创建自己的 ProcessStartInfo 实例并使用 ProcessStartInfo.WorkingDirectory 属性,以便您记住正确的路径。

The issue is that you can possibly change the current working directory when doing other file operations.

You want to remember the path as the other posters have showed you, but it may be that you want to create your own ProcessStartInfo instance and use the ProcessStartInfo.WorkingDirectory property so that you can remember the correct path.

汐鸠 2024-08-31 20:36:34

尝试显式指定 OtherApp.exe 的路径。

您的打开文件对话框可能正在更改当前目录。

Try explicitly specifying the path to OtherApp.exe.

Your open file dialog may be changing the current directory.

幻想少年梦 2024-08-31 20:36:34

如果您没有明确指定文件夹,系统将在“当前工作目录”中查找该进程。

当前工作目录(通常)以应用程序 exe 目录开始,但可以通过使用“打开”或“保存”对话框浏览来更改。

使用显式文件路径是正确的答案。最好的方法是根本不依赖工作目录,而是使用当前可执行文件的文件路径作为基础。

以下是执行此操作的一些方法: Application.StartupPathApplication.ExecutablePath

代码可能看起来像这样......

var exeName = "sample.exe";
var exePath
    = Path.Combine(
        Path.GetDirectoryName( Application.ExecutablePath ),
        exeName);

If you don't specify the folder explicitly, the system will look in the "current working directory" for the process.

The current working directory (usually) starts off as the application exe directory, but can be changed by browsing to with an Open or Save dialog.

Using an explicit filepath is the right answer. Best way is to not rely on the working directory at all, but to use the filepath of the current executable as a base.

Here are some ways to do this: Application.StartupPath, Application.ExecutablePath

Code might look something like this ...

var exeName = "sample.exe";
var exePath
    = Path.Combine(
        Path.GetDirectoryName( Application.ExecutablePath ),
        exeName);
小猫一只 2024-08-31 20:36:34

尝试 System.IO.Path.Combine( System.Windows.Forms.Application.StartupPath, "myfile.exe" );

如果它不是 winforms 项目 divo 的答案是最好的(imo,在这个答案)

Try System.IO.Path.Combine( System.Windows.Forms.Application.StartupPath, "myfile.exe" );

If it's not a winforms project divo's answer is best (imo, at the time of this answer)

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