从当前文件夹启动.exe有时会失败(Launching an .exe from the current folder sometimes fails)

编程入门 行业动态 更新时间:2024-10-23 20:23:21
从当前文件夹启动.exe有时会失败(Launching an .exe from the current folder sometimes fails)

我有一个应用程序启动与该应用程序位于同一文件夹中的可执行文件,方法是:

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

最满意答案

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

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");

更多推荐

本文发布于:2023-08-06 23:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457504.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有时会   文件夹   exe   Launching   folder

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!