Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# GnuPG加密无法使用进程错误代码2_C#_Process_Gnupg - Fatal编程技术网

C# GnuPG加密无法使用进程错误代码2

C# GnuPG加密无法使用进程错误代码2,c#,process,gnupg,C#,Process,Gnupg,我有下面的代码,当通过命令提示符调用此加密代码时(创建加密文件),此代码可以正常工作,而在控制台应用程序中则不起作用 var destFilePath = @"D:\test.gpg"; var recipient = "test@test.com"; var sourceFilePath = @"D:\test.txt"; var proc = new Process { StartInfo = new ProcessStartInfo

我有下面的代码,当通过
命令提示符调用此加密代码时(创建加密文件),此代码可以正常工作,而在
控制台应用程序中则不起作用

var destFilePath = @"D:\test.gpg";
var recipient = "test@test.com";
var sourceFilePath = @"D:\test.txt";
var proc = new Process
           {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "cmd.exe",
                    Arguments = string.Format("gpg2 --output {0} --encrypt --recipient {1} {2}",
                                                destFilePath, recipient, sourceFilePath),
                    UseShellExecute = false,
                    RedirectStandardError = true,
                    RedirectStandardInput = true,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true,
                    WorkingDirectory = @"C:\Program Files (x86)\GNU\GnuPG"
                }
            };

            proc.Start();
            proc.WaitForExit();
            int rc = proc.ExitCode;
            proc.Close();
            Console.WriteLine(rc.ToString());
            Console.ReadKey();
在ExitCode中返回错误代码2

任何想法都会有帮助

来自:

GPG询问您是否要继续加密 使用无符号键。由于没有用户可以输入Y,因此会产生错误

要解决此问题,请使用以下开关

--是
--始终信任

我已经有一段时间没有使用GPG了,但是参数的顺序通常并不重要,只要它们是命名的

这会给你类似的东西

FileName = "gpg2.exe",
Arguments = $"--output {destFilePath} --encrypt --yes --always-trust --recipient {recipient} {sourceFilePath}",

注意:这是C#6格式,更容易阅读

您需要调用gpg2 exe而不是cmd.exe错误消息是什么?为什么很多人在寻求帮助时总是忽略他们收到的实际错误消息?另外,您可能需要对变量进行分隔,所以在周围添加“”them@MikeT,在控制台中,输出为
2
请查看此处