Solved

Running an EXE file and returing the output from C#

Posted on 2011-09-08
11
634 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
 
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
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 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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

932 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now