Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Executing command-line EXE and retrieving output

0 views
Skip to first unread message

Phil Derksen

unread,
Sep 6, 2001, 7:30:27 PM9/6/01
to
I need to fire off an EXE from the command line, then retrieve the output it
gives me in the DOS window.

To start the EXE, I found from other postings that this will work:
System.Diagnostics.Process.Start("foo.exe","1 2 3 4")

How do I retrieve the output, which will be straight text, which I then want
to parse and eventually display on an ASP.NET page.

C# or VB code is fine. Thanks in advance.

Phil


Patrick Steele

unread,
Sep 6, 2001, 9:22:18 PM9/6/01
to
In article <Ov3j1vyNBHA.576@tkmsftngp05> (from Phil Derksen
<pder...@yahoo.com>),

> To start the EXE, I found from other postings that this will work:
> System.Diagnostics.Process.Start("foo.exe","1 2 3 4")
>
> How do I retrieve the output, which will be straight text, which I then want
> to parse and eventually display on an ASP.NET page.

Here's a snippet I collected from the DOTNET mailing list:

<codeSnippet language="C#">
Process commandPrompt = Process.Start("cmd.exe");
string dirResults;

commandPrompt.StandardInput.WriteLine("dir")
dirResults = commandPrompt.StandardOutput.ReadToEnd()
</codeSnippet>

--
Patrick Steele

Phil Derksen

unread,
Sep 6, 2001, 11:02:27 PM9/6/01
to
That didn't quite work. I'm trying it as a console application first before
going to an ASP.NET page. Anyway, I keep getting:

An unhandled exception of type 'System.NullReferenceException' occurred in
ConsoleTest.exe
Additional information: Value null was found where an instance of an object
was required.

I tried other variations of your code below...but kept getting the same
error.

Phil

"Patrick Steele" <pst...@ipdsolution.com_> wrote in message
news:MPG.1601eb517...@msnews.microsoft.com...

Christian Mairoll (Anti-Trojan.NET)

unread,
Sep 7, 2001, 2:12:01 AM9/7/01
to
hi phil,

try this:
// execute an application, return output string
string ExecuteCmdLineApp(string strCmd)
{
string output = "";
string error = "";

TempFileCollection tf = new TempFileCollection();
Executor.ExecWaitWithCapture(strCmd, tf, ref output, ref error);

StreamReader sr = File.OpenText(output);
StringBuilder strBuilder = new StringBuilder();
string strLine = null;

while (null != (strLine = sr.ReadLine()))
{
if ("" != strLine)
{
strBuilder.Append(strLine);
strBuilder.Append("\r\n");
}
}
sr.Close();

File.Delete(output);
File.Delete(error);

return strBuilder.ToString();
}

the source is from an german asp-page:
http://www.aspheute.com/artikel/20010220.htm


chris

"Phil Derksen" <pder...@yahoo.com> wrote in message
news:Ov3j1vyNBHA.576@tkmsftngp05...

Willy Denoyette

unread,
Sep 7, 2001, 2:58:21 AM9/7/01
to
Here's a sample, redirecting stdout to a string.
....
string dirResults;
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "cmd.exe";
psi.RedirectStandardInput = false;
psi.RedirectStandardOutput = true;
psi.Arguments = "/c dir c:\\"; // specify your command
psi.UseShellExecute = false;
Process proc = Process.Start(psi);
dirResults = proc.StandardOutput.ReadToEnd(); // read from stdout
// do something with result stream
proc.WaitForExit(); ;

Willy.

"Phil Derksen" <pder...@yahoo.com> wrote in message news:Ov3j1vyNBHA.576@tkmsftngp05...

Phil Derksen

unread,
Sep 7, 2001, 4:07:48 AM9/7/01
to
Thanks for everyone's feedback. However, a few things have changed. I found
out the external EXE I am calling (which happens to be qstat.exe from
www.qstat.org), can generate text or XML files instead of just outputting
back to the cmd window. I want to re-generate the XML file each time the
page is called, then read it into a datagrid. However, it seems as though
the asp.net page doesn't wait for the EXE to generate a file. I've tried
many different things (Process, Shell, Executor, Console) and can't get it
to work. How do I make the page delay while it's generating the XML file?

"Phil Derksen" <pder...@yahoo.com> wrote in message
news:Ov3j1vyNBHA.576@tkmsftngp05...

Peter Wagner

unread,
Sep 7, 2001, 4:15:14 AM9/7/01
to

--

"Phil Derksen" <pder...@yahoo.com> schrieb im Newsbeitrag
news:Ov3j1vyNBHA.576@tkmsftngp05...


>
> How do I retrieve the output, which will be straight text, which I
then want
> to parse and eventually display on an ASP.NET page.
>
> C# or VB code is fine. Thanks in advance.

Try it with DOS:
foo.exe 1 2 3 4 >output.txt
;-)

>
> Phil
>
>

p


Michael Giagnocavo

unread,
Sep 7, 2001, 1:21:18 PM9/7/01
to
Make a new process, then use the Wait methods.
-mike

"Phil Derksen" <pder...@mediaone.net> wrote in message
news:ukEj6O3NBHA.2036@tkmsftngp03...

0 new messages