cli*_*li2 1 .net c# linux process path
我有一个问题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。
process.StartInfo.WorkingDirectory在这种情况下,的含义不是可执行文件的位置。
这就是导致您正在经历的行为的原因,并且含义process.StartInfo.WorkingDirectory根据 的值而变化process.StartInfo.UseShellExecute。
来自微软文档:
当 UseShellExecute 为 true 时和 UseShellExecute 为 false 时,WorkingDirectory 属性的行为有所不同。
当 UseShellExecute 为 false 时,WorkingDirectory 属性不用于查找可执行文件。相反,它的值适用于已启动的流程,并且仅在新流程的上下文中才有意义。
UseShellExecutedotnet core 中的默认值为false。我在 Linux 上设置为 true 时遇到了奇怪的事情UseShellExecute。
就我个人而言,我会保留它 false 并确保使用完整路径或相对于项目根目录的路径,如下所示:
process.StartInfo.FileName="bin/Debug/netcoreapp2.1/subdir/a.out";
| 归档时间: |
|
| 查看次数: |
3646 次 |
| 最近记录: |