471,337 Members | 1,158 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

Process ExitCode 1 for batch file execution from C#

I have created a very simple batch file and trying to retrieve the
standard output but everytime I run the code it returns ExitCode as 1.

I have created a batch file as simple as ping localhost or echo hello
world and neither have worked. Note: This is from ASP.Net code. Also,
the batch file runs just fine from command line.

I am running another Exe process with arguments from the same method
and that runs just fine too.

Am I missing on something?

Here is the code:
Calling code:

ExecuteProcess( Server.MapPath( pathofbatchfile ), string.Empty, null,
null, out OutputVal );
Method implementation:

public static int ExecuteProcess ( string ProcessName, string
ProcessArguments, NameValueCollection Variables, string
WorkingDirectory ,out string Log )
{
//Clear log.
Log = "";
using ( Process DOSProcess = new Process() )
{
ProcessStartInfo StartInfo = new ProcessStartInfo();

StartInfo.FileName = ProcessName;

if ( Variables != null )
{
foreach( string Key in Variables.Keys )
{
StartInfo.EnvironmentVariables.Add( Key, Variables[ Key ] );
}
}

StartInfo.RedirectStandardError = false;
StartInfo.RedirectStandardOutput = true;
StartInfo.RedirectStandardInput = false;

StartInfo.UseShellExecute = false;
StartInfo.CreateNoWindow = true;

if ( ProcessArguments != string.Empty & ProcessArguments != null )
StartInfo.Arguments = ProcessArguments;

if ( WorkingDirectory != string.Empty & WorkingDirectory != null )
StartInfo.WorkingDirectory = WorkingDirectory;

DOSProcess.EnableRaisingEvents = true;

DOSProcess.StartInfo = StartInfo;

DOSProcess.Start();

do
{
Log += DOSProcess.StandardOutput.ReadToEnd();
}
while ( !DOSProcess.HasExited );

int ProcessExitCode = DOSProcess.ExitCode ;

return ProcessExitCode;
}

}

Dec 3 '05 #1
3 8866
Hi,

I'm having a similar problem. Did you find a solution?

"Zeya" <ze****@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
I have created a very simple batch file and trying to retrieve the
standard output but everytime I run the code it returns ExitCode as 1.

I have created a batch file as simple as ping localhost or echo hello
world and neither have worked. Note: This is from ASP.Net code. Also,
the batch file runs just fine from command line.

I am running another Exe process with arguments from the same method
and that runs just fine too.

Am I missing on something?

Here is the code:
Calling code:

ExecuteProcess( Server.MapPath( pathofbatchfile ), string.Empty, null,
null, out OutputVal );
Method implementation:

public static int ExecuteProcess ( string ProcessName, string
ProcessArguments, NameValueCollection Variables, string
WorkingDirectory ,out string Log )
{
//Clear log.
Log = "";
using ( Process DOSProcess = new Process() )
{
ProcessStartInfo StartInfo = new ProcessStartInfo();

StartInfo.FileName = ProcessName;

if ( Variables != null )
{
foreach( string Key in Variables.Keys )
{
StartInfo.EnvironmentVariables.Add( Key, Variables[ Key ] );
}
}

StartInfo.RedirectStandardError = false;
StartInfo.RedirectStandardOutput = true;
StartInfo.RedirectStandardInput = false;

StartInfo.UseShellExecute = false;
StartInfo.CreateNoWindow = true;

if ( ProcessArguments != string.Empty & ProcessArguments != null )
StartInfo.Arguments = ProcessArguments;

if ( WorkingDirectory != string.Empty & WorkingDirectory != null )
StartInfo.WorkingDirectory = WorkingDirectory;

DOSProcess.EnableRaisingEvents = true;

DOSProcess.StartInfo = StartInfo;

DOSProcess.Start();

do
{
Log += DOSProcess.StandardOutput.ReadToEnd();
}
while ( !DOSProcess.HasExited );

int ProcessExitCode = DOSProcess.ExitCode ;

return ProcessExitCode;
}

}

Jan 2 '06 #2
Yes.

Set Shell execute to true.

StartInfo.UseShellExecute = true;

This MAY stop standard output.

HTH.

Jan 3 '06 #3
It didn't work for me. Thanks anyway I'll try to send a new post with the
specific problem.

"Zeya" <ze****@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Yes.

Set Shell execute to true.

StartInfo.UseShellExecute = true;

This MAY stop standard output.

HTH.

Jan 5 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dmitri Shvetsov | last post: by
1 post views Thread by Bill Rapoza | last post: by
1 post views Thread by Andy | last post: by
1 post views Thread by stemp1ar | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.