System.Process

  • Thread starter Thread starter DaveL
  • Start date Start date
D

DaveL

hello
Im using System.Diagnostics.Process to Run
a External Exe

When i Run it its all good except sometimes
the Executed Process running askes for a Yes or No
how can i send Keys to a Shelled Process to make it continue

DaveL
 
Im working on it now...but i have one question about sendkeys i use that in
windows client classes
but this could be used as a back end object from web site...
Im actually calling pgp -e file2encrypt skey

if the key is not signed it will ask whether to use the key or not
I want to send Y and Enter to continue....

System.Windows.forms. ive had errors from the websever when this namespace
is used

Code will be executed on the server not the client


Thanks DaveL
 
Im just using xcopy one folder to another
"xcopy d:\temp\*.* /s d:\junk"
this will produce the overwrite yes/no/All
i Cant Capture the Question Text

DaveL
 
below is some code i got to work....any sugestions welcome to make it
better....
Im just using xcopy d:\temp\*.* /s d:\junk this will produce the
Yes/No/All
to overwrite existing files
I was unable to use proc.standardoutput.readLine() to capture the output
would just hang
so i tried the Read() method and this caught all the start up text of the
running app (xcopy) output
before it actually started to copy files
I dont have PGP.exe here at hope to test with that so i used xcopy to
emulate pgp
Pgp will ask when a key is not signed wether to use the encryptionkey or not
so i will need to Send a Y to Pgp

Thanks for any advice for the below code
DaveP



public void ReadStandardOut()
{
try
{
char c ;
int iChar;
string line="";
//read all characters from running application
//if the string looking for is found break out of loop

while ( (iChar=proc.StandardOutput.Read())>0 &&
proc.HasExited==false)
{
line+=((char)iChar).ToString();
if (line.IndexOf("Yes/No") > 0)
{

break;
}
}
//if above string not found check HasExited...if false Write A
for overwrite all
//then read the files being copied
if (proc.HasExited == true)
{
return;
}

proc.StandardInput.WriteLine("A"); //write the A for
overwrite All to Xcopy
// string line;
while ((!proc.HasExited) && ((line =
proc.StandardOutput.ReadLine()) != null))

Console.WriteLine("Line "+line);
}
catch (InvalidOperationException)
{
Console.WriteLine("here error");
// The process has exited or StandardError hasn't been
redirected.
}
}



}
 
im not gonna use xcopy
I will be using PGP.exe
Im just using xcopy to get the Question....

daveL
 
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;
}

}


}
 
Back
Top