1

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.

matts
  • 6,738
  • 1
  • 33
  • 50
Si8
  • 9,141
  • 22
  • 109
  • 221
  • 1
    Calling a java program from a batch file from a process started in a C# program ... I doubt this is the correct approach for the problem. – clcto Jun 17 '14 at 15:00
  • What does `com.test.this` actually do? Could you duplicate it's function in C#? – Chris Dunaway Jun 17 '14 at 15:14
  • It has been working so far, just didn't want to call the batch file and embed it in my C# application instead. – Si8 Jun 17 '14 at 15:25
  • @ChrisDunaway Unfortunately it is a vendor application and not compatible with any C# code. – Si8 Jun 17 '14 at 15:26

2 Answers2

2

It may be enough to:

var pro = Process.Start("java", "com.test.this send");

Considering that you have @ECHO ON, I presume you want to read an output of the process (in case it redirected from the inside of it).

For this read : Process.start: how to get the output?

If this is not what you are asking for, please clarify.

Community
  • 1
  • 1
Tigran
  • 61,654
  • 8
  • 86
  • 123
  • With my code I am already doing it. I can read the output and save it to a string to be used at a later time within the application. I just want to eliminate the reading the bat file and have it embedded within my C# code. – Si8 Jun 17 '14 at 15:01
  • @SiKni8: so try to invoce it like in linked example. – Tigran Jun 17 '14 at 15:01
  • That seems like what I am looking to do, how can I add multiple arguements? – Si8 Jun 17 '14 at 15:02
  • @SiKni8: according to the documentation, it's a single string so just pass them in a single string, like from example. – Tigran Jun 17 '14 at 15:03
  • I updated my question with what I have and I get a `.` as a messagebox. – Si8 Jun 17 '14 at 15:26
  • You may want to redirect and capture StdError also – TaW Jun 17 '14 at 15:57
1

About, how do I do it silently, so the user doesn't see the CMD window and the operation takes place in the background?

You can suppress new window by setting this property of ProcessStartInfo.

CreateNoWindow = true
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103