I have the following code which call and execute a batch file,
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = @"C:\tS\comm.bat";
proc.StartInfo.RedirectStandardError = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
string strGetInfo = proc.StandardOutput.ReadToEnd();
strCMDOut = strGetInfo.Substring(strGetInfo.Length - 5, 3);
//MessageBox.Show(cmdOut);
proc.WaitForExit();
The comm.bat
file contains the following,
@ECHO ON
java com.test.this send
Instead of calling a file to execute the bat file, how can I incorporate it within my C# code and prevent any file issue? (I need the output to be saved to a string as it is already doing it in the above code.)
Also, how do I do it silently, so the user doesn't see the CMD window and the operation takes place in the background?
I replaced the code with this:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "java com.test.this send",
RedirectStandardError = false,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
string strGetInfo = proc.StandardOutput.ReadToEnd();
strCMDOut = strGetInfo.Substring(strGetInfo.Length - 5, 3);
MessageBox.Show(strCMDOut);
I get a .
in the message box instead of the code that the original code was displaying.