?
Solved

Running an EXE file and returing the output from C#

Posted on 2011-09-08
11
Medium Priority
?
641 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:James Atkin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 2
  • 2
  • +1
11 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 400 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:James Atkin
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
Put Machine Learning to Work--Protect Your Clients

Machine learning means Smarter Cybersecurity™ Solutions.
As technology continues to advance, managing and analyzing massive data sets just can’t be accomplished by humans alone. It requires huge amounts of memory and storage, as well as the high-speed power of the cloud.

 
LVL 19

Assisted Solution

by:NickUpson
NickUpson earned 400 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:James Atkin
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 86

Accepted Solution

by:
Mike Tomlinson earned 1200 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:James Atkin
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 86

Expert Comment

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

Author Comment

by:James Atkin
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:James Atkin
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:James Atkin
ID: 36547831
Thanks to all of you for the suggestions - hope the points split is ok for you all...
James
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This article investigates the question of whether a computer can really be cleaned once it has been infected, and what the best ways of cleaning a computer might be (in this author's opinion).
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

800 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