below is a test class i put together to run pgp.exe
Cant seem to get pgp to go after a unsignd key
When a key is not signed/trusted pgp will ask Continue (y/N)?
I Do Capture the y/n)?
then Write standardinput.WriteLine("Y") but can't seem to get it to work
the Test Class is Below
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace batchPm160
{
class Program
{
static void Main(string[] args)
{
PgpWrapper oPgp = new PgpWrapper()
oPgp.Pgp("d:\tempt\test.txt "+mykey,false) //false is for
ascii armor
}
}
public class PgpWrapper:Object
{
private System.Diagnostics.Process oProcess;
public int Seconds = 20;
public PgpWrapper()
:base()
{
this.oProcess= new Process();
}
public int Pgp(string sFilePath, string sPgpKey,bool bUseAsciiArmor)
{
int iRet = 1;
this.oProcess.StartInfo.FileName = "Pgp.exe";
// this.oProcess.StartInfo.WindowStyle =
System.Diagnostics.ProcessWindowStyle.Hidden;
if (System.IO.File.Exists(sFilePath+".PGP")==true)
{
System.IO.File.Delete(sFilePath+".PGP");
}
if (bUseAsciiArmor==true)
{
this.oProcess.StartInfo.Arguments = "-e -a " + sFilePath+"
"+sPgpKey + " ";
}
else
{
this.oProcess.StartInfo.Arguments = "-e " + sFilePath+"
"+sPgpKey + " ";
}
//ThisProcess.StartInfo.Verb = "OPEN";
this.oProcess.StartInfo.UseShellExecute = false;
this.oProcess.StartInfo.RedirectStandardInput = true;
this.oProcess.StartInfo.RedirectStandardOutput = true;
this.oProcess.StartInfo.CreateNoWindow = true;
this.oProcess.EnableRaisingEvents = true;
Console.WriteLine(this.oProcess.Start());
//oW.Write("Y"+(char)13);
if (this.Wait4TimedExit(this.Seconds) == false)
{
iRet = -1;
}
return iRet;
}
public bool Wait4TimedExit(int TimeInSeconds)
{
bool Success = false;
try
{
int iChar;
string line = "";
//read all characters from running application
//if the string looking for is found break out of loop
while ((iChar = this.oProcess.StandardOutput.Read()) > 0 &&
this.oProcess.HasExited == false)
{
line += ((char)iChar).ToString();
//Console.WriteLine(line);
if (line.IndexOf("y/N)?") > 0)
{
Console.WriteLine("Got it");
break;
}
}
if (this.oProcess.HasExited == true)
{
Console.WriteLine("Exited ");
return true;
}
this.oProcess.StandardInput.WriteLine("Y"+this.oProcess.StandardInput.NewLine);
// Success = (this.oProcess.WaitForExit(TimeInSeconds *
1000));
}
catch (InvalidOperationException e)
{
string cMsg;
//use e object do somthing later or remove this error
handler
cMsg = e.Data.ToString();
Console.WriteLine(cMsg);
Console.ReadKey();
Success = false;
}
this.oProcess.Close();
return Success;
}
}
}