Search code examples
c#windowswindows-servicesgnupg

Error when running gpg.exe from c#


Hi I'm trying to encrypt the zip using the GPG im able to do that from a command line. When i integrated that in a C# application it worked well. But when i integrated it with a Windows service i got Error as gpg2.exe closed

Following are the exception details

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

Following is the code i'm using to encrypt

public bool Encrypt(string inRecipient, string sourceFile, string 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;

}

Im not able to find anything using the Problem signature. Please help

Thanks in advance!!


Solution

  • This is most likely caused by a user context problem. Probably the user account the services is running under doesn't have access to the file you are trying to encrypt.

    To test this theory, go to the service properties and in the 'Log On' tab enter an account that you know has access to the file. Preferably the same account/password that you are using to run the C# application

    You should also be sure that your code is using a full path to the file to encrypt and not a relative path.