Solved

Running an EXE file and returing the output from C#

Posted on 2011-09-08
11
633 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

UPDATE - 6/15/2011 Added support for Release Update 6 Maintenance Patch 2 Point Patch 1 (RU6 MP2 PP1). Fixed a defect in the username field that was hard-coded to look for a specific domain (left over code from testing). This release will be the …
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 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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

759 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

23 Experts available now in Live!

Get 1:1 Help Now