I have a very strange problem here. I have a wrapper class for a
command line MyProgram.exe. When i run it from the command prompt
(MyProgram.exe), it runs perfectly. So there is not problem with it
for sure. When I fire some command from my C# program to this exe, I
get the response for ALMOST all the commands perfectly.
BUT For a few commands, my program sometimes gives the desired output,
and sometimes only half of it. (as it gives at command prompt). AND
sometime, process doesnot return any output after I fire certain
commands. I am not able to figure out where is the problem and why It
is occuring. This is happening for any command and not for any
specific command.
I am posting the code below. I would really appreciate help on these.
------------------------------------------------------------------
....
....
....
private Process m_pro = new Process();
ProcessStartInfo myInfo = new ProcessStartInfo("MyProgram.exe");
myInfo.Arguments = " /u " + UserName +
" /p " + Password;
myInfo.UseShellExecute=false;
myInfo.RedirectStandardOutput=true;
myInfo.RedirectStandardInput=true;
myInfo.RedirectStandardError = true;
myInfo.CreateNoWindow=true;
myInfo.WorkingDirectory = InstallDir;
m_pro.StartInfo=myInfo;
m_pro.Start();
....
....
....
string myString = "";
string tmp = "";
bool skip = false;
try
{
StreamReader m_readStream = m_pro.StandardOutput;
m_pro.StandardInput.Write(command + "\n\n");
m_pro.StandardInput.Flush();
//--- HERE in below code m_readStream does not give any output
SOMETIMES.
//-- MOST of the time it works fine.
// Remove the prompt
while (m_readStream.Peek() >=0)
{
tmp = m_readStream.ReadLine();
if (tmp.IndexOf("prompt>")>=0)
break;
}
while (m_readStream.Peek() >=0)
{
tmp = m_readStream.ReadLine();
if (tmp == "") { skip = true;}
if (!skip)
myString = myString + tmp + "\r\n";
if (tmp.IndexOf("prompt>")>=0)
break;
}
m_pro.Refresh();
return myString;
}