我无法使用 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)
Run Code Online (Sandbox Code Playgroud)
我已经设置了WorkingDirectory = "path/lib"和"FileName = "../bin/a.out"。但是我收到一个错误:
Unhandled Exception: System.ComponentModel.Win32Exception: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我尝试设置WorkingDirectory为绝对和相对路径,但都不起作用。我已经编写了一个 bash 脚本来从 lib 目录执行 a.out,并使用我调用 bash 脚本的 Process 类,这是可行的,但我想在没有 bash 脚本解决方法的情况下执行此操作。那么如何解决这个路径问题呢?
我有一个问题WorkingDirectory,它没有正确设置所需的路径。我编写了一个简单的 hello world 测试程序a.out来尝试一下WorkingDirectory。目录层次结构是这样的:
/home/cli2/test
/bin/Debug/netcoreapp2.1/subdir/
a.out
/obj
Program.cs
test.csproj
Run Code Online (Sandbox Code Playgroud)
我对 Process 类有以下设置
process.StartInfo.FileName = "a.out"
process.StartInfo.UseShellExecute = false;
process.StartInfo.WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/subdir/";
Run Code Online (Sandbox Code Playgroud)
当我执行时dotnet run,我会收到以下错误:
Unhandled Exception: System.ComponentModel.Win32Exception: No such file or directory
Run Code Online (Sandbox Code Playgroud)
让我困惑的问题是,如果我移动a.out到顶级目录,这样:
/home/cli2/project
/bin/Debug/netcoreapp2.1/subdir/
/obj
Program.cs
test.csproj
a.out
Run Code Online (Sandbox Code Playgroud)
在具有相同StartInfo设置的情况下,process.start()执行 hello world 程序不会出现错误。此外,如果我更改FileName = "ls"原始子目录层次结构,它会打印出a.out. 对于这种情况,其WorkingDirectory行为符合预期。所以我确实理解这种差异以及为什么我不能a.out在不同的目录中调用。
另外,我尝试了绝对路径和相对路径WorkingDirectory,但当我调用时都不起作用a.out。