Hi Friends,
The basic issue is "To read stdin and stdout in the same program" The following program is i have wrote using thread but i figured out that stdout is working but it is not working when application need the input.
'myprog.pl' is the simple perlscript which ask for the your name and print the same. the execution sequence is given below:
c:\ myprog.pl
Enter your name: Myname
Your name is: Myname
When i am running this script from my C# prog given below it it is not asking for the name at all and it is giving following output;
C:\myCSharpProg .exe
stdin
stdout
Enter your name:Your name is :
Could anyone please help me on this, it will be veryhelpful to me if any alternative method is there to achieve this.
Thanks in Advanced,
Megha
The basic issue is "To read stdin and stdout in the same program" The following program is i have wrote using thread but i figured out that stdout is working but it is not working when application need the input.
'myprog.pl' is the simple perlscript which ask for the your name and print the same. the execution sequence is given below:
c:\ myprog.pl
Enter your name: Myname
Your name is: Myname
When i am running this script from my C# prog given below it it is not asking for the name at all and it is giving following output;
C:\myCSharpProg .exe
stdin
stdout
Enter your name:Your name is :
Could anyone please help me on this, it will be veryhelpful to me if any alternative method is there to achieve this.
Thanks in Advanced,
Megha
Code:
public void ProgramExecute(
string password,
string commandPath,
string commandArguments)
{
process = new Process();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = " /c \"" + " myprog.pl";
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.Verb = "Open";
process.StartInfo.UseShellExecute = false;
process.StartInfo.ErrorDialog = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.LoadUserProfile = true;
// get the domain and user name parts of the current
// windows identity
Match identity_match = Regex.Match(
WindowsIdentity.GetCurrent().Name,
@"^([^\\]+)\\(.+)$");
// domain name
string dn = identity_match.Groups[1].Value;
// user name
string un = identity_match.Groups[2].Value;
// only set the domain if it is an actual domain and
// not the name of the local machine, i.e. a local account
// invoking sudo
if (!Regex.IsMatch(dn,
Environment.MachineName, RegexOptions.IgnoreCase))
{
process.StartInfo.Domain = dn;
}
process.StartInfo.UserName = un;
// transform the plain-text password into a
// SecureString so that the ProcessStartInfo class
// can use it
process.StartInfo.Password = new System.Security.SecureString();
for (int x = 0; x < password.Length; ++x)
process.StartInfo.Password.AppendChar(password[x]);
process.Start();
new Thread(new ThreadStart(this.readStdin)).Start();
new Thread(new ThreadStart(this.readStdout)).Start();
process.WaitForExit();
Environment.Exit(process.ExitCode);
}
private void readStdin()
{
try
{
Console.WriteLine("stdin");
string line;
while ((line = Console.In.ReadLine()) != null)
{
process.StandardInput.WriteLine(line);
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
}
private void readStdout()
{
try
{
Console.WriteLine("stdout");
int b;
while ((b = process.StandardOutput.BaseStream.ReadByte()) != -1)
{
Console.Write((char)b);
}
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
}
Comment