Solved

Running an EXE file and returing the output from C#

Posted on 2011-09-08
11
636 Views
Last Modified: 2013-12-17
Hi all,

I have a C# (2010) application that I wish to run an EXE file and return the output text.
The partucular EXE is used to get version information which I would like to extract.

In a BAT file I have the following:

    @echo off
    C:\CodeGear\InterBase\bin\gsec.exe -user sysdba -password masterkey -z -quit

When i run this manually, I get the following output:

    C:\>test.bat
    gsec version  WI-V9.0.3.437
   
    C:\>

however, when run from the following code, I get nothing? I am missing something!!!
It does appear to be running as when I put 'pause' in the batch file, I get "Press any key" returned to the app!
Please help me,
James
Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Test.Bat";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.WaitForExit();
string xResponse = p.StandardOutput.ReadToEnd();
MessageBox.Show(xResponse);

Open in new window

0
Comment
Question by:jatkin
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 100 total points
ID: 36501402
Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "C:\\Test.Bat";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.Start();

string xResponse = p.StandardOutput.ReadToEnd();
p.WaitForExit();

MessageBox.Show(xResponse);

Does that do the trick?
0
 
LVL 4

Author Comment

by:jatkin
ID: 36501551
Hi,
I did try this originally but it did not work either.
The strange thing is that when PAUSE is put at the end of the batch file, the result is passed back to the C# app...
Are there any issues with security for .NET running BAT files that is maybe causing the EXE file to not be run when being called from the C# app?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 36501574
what happens if you don't have the @echo off line in the bat file ?


Security- that I can't answer.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 19

Assisted Solution

by:NickUpson
NickUpson earned 100 total points
ID: 36501834
what happens when you run

C:\CodeGear\InterBase\bin\gsec.exe -user sysdba -password masterkey -z -quit

yourself on the commandline? are you sure that the sysdba password is masterkey?
0
 
LVL 4

Author Comment

by:jatkin
ID: 36501901
Andy, I will try without echo shortly...

Nick, I have verified that running the same Batch file gives the results as expected (the exact output is shown in the original message above)
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 300 total points
ID: 36503211
Why the BAT file?  What happens if you run it directly?
// C:\CodeGear\InterBase\bin\gsec.exe -user sysdba -password masterkey -z -quit
            Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = @"C:\CodeGear\InterBase\bin\gsec.exe";
            p.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(p.StartInfo.FileName);
            p.StartInfo.Arguments = "-user sysdba -password masterkey -z -quit";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardOutput = true;
            p.Start();
            p.WaitForExit();
            string xResponse = p.StandardOutput.ReadToEnd();
            MessageBox.Show(xResponse);

Open in new window

0
 
LVL 4

Author Comment

by:jatkin
ID: 36521092
Hi all,
Sorry I have had the hard drive go down in my PC!!!!!
Just getting the dev environment back up and I'll try out the solutions..
Thanks,
James
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 36522435
Fresh install!...enjoy your "new" peppy machine.  ;)
0
 
LVL 4

Author Comment

by:jatkin
ID: 36547815
Ok, thanks for the patience!!!!
My PC is back up and running:
 - Windows 7 x64 Ultimate
 - Visual Studio 2010 Pro
 - All fully Updated

I have tried the more direct approach as suggested by 'Idle Mind' but I get the same result???

However, if I cahange to Use Shell Execute, when the command prompt displays briefly, I see the version displayed!!!

Why would this not redirect?
0
 
LVL 4

Author Comment

by:jatkin
ID: 36547820
Ah!  Found what is happening - it looks like the gsec.exe application sends it response to StandardError!!!!!
Only Borland/Codegear/Embarcadero (whatever they call themselves today) could do this!!!
Thanks for all the help
0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 36547831
Thanks to all of you for the suggestions - hope the points split is ok for you all...
James
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These are on the increase and getting more common these days. Users who use the Google search engine may complain of having their search redirected to unwanted sites, regardless of what browser is used. This happens when the system is infected with…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question