0

嗨,我正在尝试使用 GPG 加密 zip,我可以从命令行执行此操作。当我将它集成到 C# 应用程序中时,它运行良好。但是当我将它与 Windows 服务集成时,gpg2.exe 关闭时出现错误

以下是异常详情

Problem signature:
  Problem Event Name:   APPCRASH
  Application Name: gpg2.exe
  Application Version:  0.0.0.0
  Application Timestamp:    4fa14f63
  Fault Module Name:    StackHash_e51a
  Fault Module Version: 0.0.0.0
  Fault Module Timestamp:   00000000
  Exception Code:   c0000005
  Exception Offset: 00000000
  OS Version:   6.0.6002.2.2.0.272.7
  Locale ID:    1033
  Additional Information 1: e51a
  Additional Information 2: 4c0d4d78887f76d971d5d00f1f20a433
  Additional Information 3: e51a
  Additional Information 4: 4c0d4d78887f76d971d5d00f1f20a433

以下是我用来加密的代码

公共布尔加密(字符串inRecipient,字符串sourceFile,字符串destinationFile){

/// File info
FileInfo fi = new FileInfo(sourceFile);

ProcessStartInfo s = new ProcessStartInfo("cmd.exe");
s.CreateNoWindow = true;
s.UseShellExecute = false;
s.RedirectStandardInput = true;
s.RedirectStandardOutput = true;
s.RedirectStandardError = true;
s.WorkingDirectory = new FileInfo(pgpPath).DirectoryName;

bool processExited = false;

using (Process p = Process.Start(s))
{

    string recipient = " --recipient \"" + inRecipient + "\"";
    string output = " --output \"" + destinationFile + "\"";
    string encrypt = " --encrypt \"" + sourceFile + "\"";
    string homedir = " --homedir \"" + HomeDirectory + "\"";
    string cmd = "\"" + PgpPath + "\" " + recipient + output + encrypt;

    p.StandardInput.WriteLine(cmd);
    p.StandardInput.Flush();
    p.StandardInput.Close();
    processExited = p.WaitForExit(3500);
    p.Close();
}
return processExited;

}

我无法使用问题签名找到任何东西。请帮忙

提前致谢!!

4

2 回答 2

3

如果要使用 Process,为什么不直接运行 gpg.exe 而不是 cmd.exe?我只是为一个项目做了这个,并且没有遇到以下问题

private static void encrypt()
    {
        //have to list full path, adding to PATH had no effect 
        ProcessStartInfo gpg = new ProcessStartInfo(
            @"C:\Program Files (x86)\GnuPT\GPG\gpg.exe",
            @"--no-options --yes --armor --recipient ""recipient"" --encrypt ""file"""
        );
        Process.Start(gpg);
    }

只需将所有命令行参数作为第二个参数传递。

于 2013-07-26T19:32:38.047 回答
2

这很可能是由用户上下文问题引起的。运行服务的用户帐户可能无权访问您尝试加密的文件。

要测试此理论,请转到服务属性并在“登录”选项卡中输入您知道可以访问该文件的帐户。最好与您用于运行 C# 应用程序的帐户/密码相同

您还应该确保您的代码使用文件的完整路径进行加密,而不是相对路径。

于 2012-09-04T17:51:49.530 回答