运行 如何在 linux 中将命令行程序作为 c#(mono) 中的单独进程?
How to run command line program as a separate process in c#(mono) in linux?
所以我正在尝试从 c# 程序中使用 7z 压缩一些文件。我已经尝试了所有可以在网上找到的可能组合。代码如下所示:
ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/usr/bin/7z", Arguments = " \"" + command.CommandText + " \"" };
Process proc = new Process() { StartInfo = startInfo, };
//proc.StartInfo.UseShellExecute = false;
//proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
//string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
我得到以下输出:
Usage:
xdg-open [OPTION...] { file | URL }
Help Options:
-h, --help Show help options
Application Options:
--version Show program version
当我 运行 终端中的完整命令字符串时,它起作用了。为什么它会尝试使用 xdg-open 打开它,为什么会失败?我还尝试通过 /bin/bash 运行 它,使用 7z(或 /usr/bin/7z)作为参数,有或没有 -c 选项,但所有这些都不起作用。谢谢!
Edit1:当我关闭 UseShellExecute 时,抛出以下异常:
InvocationException: Exception has been thrown by the target of an invocation. ---> System.ComponentModel.Win32Exception: ApplicationName='/usr/bin/7z', CommandLine=' "a /home/dusan/test/asdasd.7z /home/dusan/Profil.html "', CurrentDirectory='', Native error= Cannot find the specified file
Edit2:程序在系统中,截图如下:
另外,当我 运行 whereis 7z
,我得到
7z: /usr/bin/7z /usr/share/man/man1/7z.1.gz
另外,我已经写过它可以从终端运行。
如果我删除引号,也会出现同样的错误。
The underlying error appears to be due to the program not being on the system. Based on the chat conversation, the user has installed and is running monodevelop via flatpak, which uses a container mechanism. As a consequence the file may appear to be on his desktop, but it's not in the container that is running the monodevelop instance.
当我将 UseShellExecute=true
与伪造的可执行文件名称一起使用时,我得到:
xdg-open: unexpected argument 'a bob.7z helloworld.cs'
Try 'xdg-open --help' for more information.
当我将 UseShellExecute=false
与伪造的可执行名称一起使用时,我得到:
System.ComponentModel.Win32Exception (0x80004005): ApplicationName='/usr/bin/7zx', CommandLine=' "a bob.7z helloworld.cs" ', CurrentDirectory='', Native error= Cannot find the specified file
现在,转到传递给命令的文本 - 命令行解析器将尝试拆分内部传递的参数,因此如果传递字符串:
" \"a hello.7z file1 file2\" "
它将被解析并拆分为单个参数 a hello.7z file1 file2
,这将在 7z 命令行上失败:
Command Line Error:
Unsupported command:
a bob.7z helloworld.cs
所以你需要注意你正在使用的引用 - 如果文件中有空格,那么我会明确地引用它们,否则我会完全跳过引用。
它使用 xdg-open
的原因是 ProcessStartInfo
上似乎存在 UseShellExecute
属性 的隐式设置。你应该关闭它:
var startInfo = new ProcessStartInfo() {
FileName = "/usr/bin/7z",
Arguments = " \"" + command.CommandText + " \"",
UseShellExecute = false
};
我无法从我的单声道实例中重现此问题,但它正在调用 xdg-open
的事实意味着 this is the case.
所以我正在尝试从 c# 程序中使用 7z 压缩一些文件。我已经尝试了所有可以在网上找到的可能组合。代码如下所示:
ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/usr/bin/7z", Arguments = " \"" + command.CommandText + " \"" };
Process proc = new Process() { StartInfo = startInfo, };
//proc.StartInfo.UseShellExecute = false;
//proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
//string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
我得到以下输出:
Usage:
xdg-open [OPTION...] { file | URL }
Help Options:
-h, --help Show help options
Application Options:
--version Show program version
当我 运行 终端中的完整命令字符串时,它起作用了。为什么它会尝试使用 xdg-open 打开它,为什么会失败?我还尝试通过 /bin/bash 运行 它,使用 7z(或 /usr/bin/7z)作为参数,有或没有 -c 选项,但所有这些都不起作用。谢谢!
Edit1:当我关闭 UseShellExecute 时,抛出以下异常:
InvocationException: Exception has been thrown by the target of an invocation. ---> System.ComponentModel.Win32Exception: ApplicationName='/usr/bin/7z', CommandLine=' "a /home/dusan/test/asdasd.7z /home/dusan/Profil.html "', CurrentDirectory='', Native error= Cannot find the specified file
Edit2:程序在系统中,截图如下:
另外,当我 运行 whereis 7z
,我得到
7z: /usr/bin/7z /usr/share/man/man1/7z.1.gz
另外,我已经写过它可以从终端运行。 如果我删除引号,也会出现同样的错误。
The underlying error appears to be due to the program not being on the system. Based on the chat conversation, the user has installed and is running monodevelop via flatpak, which uses a container mechanism. As a consequence the file may appear to be on his desktop, but it's not in the container that is running the monodevelop instance.
当我将 UseShellExecute=true
与伪造的可执行文件名称一起使用时,我得到:
xdg-open: unexpected argument 'a bob.7z helloworld.cs'
Try 'xdg-open --help' for more information.
当我将 UseShellExecute=false
与伪造的可执行名称一起使用时,我得到:
System.ComponentModel.Win32Exception (0x80004005): ApplicationName='/usr/bin/7zx', CommandLine=' "a bob.7z helloworld.cs" ', CurrentDirectory='', Native error= Cannot find the specified file
现在,转到传递给命令的文本 - 命令行解析器将尝试拆分内部传递的参数,因此如果传递字符串:
" \"a hello.7z file1 file2\" "
它将被解析并拆分为单个参数 a hello.7z file1 file2
,这将在 7z 命令行上失败:
Command Line Error:
Unsupported command:
a bob.7z helloworld.cs
所以你需要注意你正在使用的引用 - 如果文件中有空格,那么我会明确地引用它们,否则我会完全跳过引用。
它使用 xdg-open
的原因是 ProcessStartInfo
上似乎存在 UseShellExecute
属性 的隐式设置。你应该关闭它:
var startInfo = new ProcessStartInfo() {
FileName = "/usr/bin/7z",
Arguments = " \"" + command.CommandText + " \"",
UseShellExecute = false
};
我无法从我的单声道实例中重现此问题,但它正在调用 xdg-open
的事实意味着 this is the case.