所以我试图用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
另外,我已经写过它可以在终端上运行。 如果删除引号,则会出现相同的错误。
答案 0 :(得分:0)
潜在的错误似乎是由于程序不在系统中。基于聊天对话,用户已经通过flatpak安装并运行monodevelop,后者使用容器机制。因此,该文件可能看起来在他的桌面上,但它不在运行monodevelop实例的容器中。
当我使用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
的原因是UseShellExecute
上似乎隐含了ProcessStartInfo
属性设置。你应该关闭它:
var startInfo = new ProcessStartInfo() {
FileName = "/usr/bin/7z",
Arguments = " \"" + command.CommandText + " \"",
UseShellExecute = false
};
我无法从我的mono实例中重现此问题,但它调用xdg-open
的事实意味着this is the case。