[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 644
  • Last Modified:

Running an EXE file and returing the output from C#

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
James Atkin
Asked:
James Atkin
  • 6
  • 2
  • 2
  • +1
3 Solutions
 
AndyAinscowCommented:
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
 
James AtkinSenior Principle Software EngineerAuthor Commented:
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
 
AndyAinscowCommented:
what happens if you don't have the @echo off line in the bat file ?


Security- that I can't answer.
0
 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

 
NickUpsonCommented:
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
 
James AtkinSenior Principle Software EngineerAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
James AtkinSenior Principle Software EngineerAuthor Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Fresh install!...enjoy your "new" peppy machine.  ;)
0
 
James AtkinSenior Principle Software EngineerAuthor Commented:
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
 
James AtkinSenior Principle Software EngineerAuthor Commented:
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
 
James AtkinSenior Principle Software EngineerAuthor Commented:
Thanks to all of you for the suggestions - hope the points split is ok for you all...
James
0

Featured Post

New Tabletop Appliances Blow Competitors Away!

WatchGuard’s new T15, T35 and T55 tabletop UTMs provide the highest-performing security inspection in their class, allowing users at small offices, home offices and distributed enterprises to experience blazing-fast Internet speeds without sacrificing enterprise-grade security.

  • 6
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now