【发布时间】:2018-10-01 21:26:40
【问题描述】:
我无法使用 Process 类调用程序来启动程序。可执行文件的层次结构在 bin 目录下,而当前工作目录需要在 lib 目录下。
/project
/bin
a.out (this is what I need to call)
/lib
(this is where I need to be in order for a.out to work)
我已经设置了WorkingDirectory = "path/lib" 和"FileName = "../bin/a.out"。但是我收到以下错误:
Unhandled Exception: System.ComponentModel.Win32Exception: No such file or directory
我尝试将WorkingDirectory 设置为绝对路径和相对路径,但都不起作用。我已经编写了一个 bash 脚本来从 lib 目录执行 a.out,并使用我调用 bash 脚本的 Process 类,这可行,但我想在没有 bash 脚本解决方法的情况下执行此操作。那么如何解决这个路径问题呢?
【问题讨论】:
-
调用
Process.Start时父进程的工作目录是什么? -
你有没有考虑过使用
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)来构建路径? -
@FlorianWeimer @mjwills,父进程在
/home/cli2/tmp/test/bin/Debug/netcoreapp2.1开始,而我想要调用的程序在/home/cli2/tmp/program/lib。使用Path.GetDirectoryName(...)为我提供了父进程路径。 -
只是补充一下,我将
FileName = ls设置为WorkingDirectory = home/cli2/tmp/program/lib,我确实看到了我想调用的程序,但是当我将 FileName 更改为该程序时,它给了我没有这样的文件或目录错误。以及File.Exists("/home/cli2/tmp/program/lib/a.out")返回true