Discussion:
Process ExitCode 1 for batch file execution from C#
(too old to reply)
Zeya
2005-12-03 03:07:23 UTC
Permalink
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;
}

}
Nurit N
2006-01-02 12:20:46 UTC
Permalink
Hi,

I'm having a similar problem. Did you find a solution?
Post by Zeya
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?
ExecuteProcess( Server.MapPath( pathofbatchfile ), string.Empty, null,
null, out OutputVal );
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;
}
}
Zeya
2006-01-03 22:06:14 UTC
Permalink
Yes.

Set Shell execute to true.

StartInfo.UseShellExecute = true;

This MAY stop standard output.

HTH.
Nurit N
2006-01-05 09:33:42 UTC
Permalink
It didn't work for me. Thanks anyway I'll try to send a new post with the
specific problem.
Post by Zeya
Yes.
Set Shell execute to true.
StartInfo.UseShellExecute = true;
This MAY stop standard output.
HTH.
Random content
2021-06-21 08:11:38 UTC
Permalink
hlooo i have CMD code exampel :
netsh winsock reset
netsh int ip reset
ipconfig /release
ipconfig /renew
ipconfig /flushdns
i wont to conver this to C# code
😉 Good Guy 😉
2021-06-21 18:17:44 UTC
Permalink
Post by Random content
netsh winsock reset
netsh int ip reset
ipconfig /release
ipconfig /renew
ipconfig /flushdns
i wont to conver this to C# code
One way is to create a batch file and then run that batch file from
within the c# program.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace cmdPrompt
{
class Program
{
static void Main()
{
Process.Start("explorer.exe", @"C:\temp\run.bat");
}
}
}


The simple batch file looks like this:

netsh winsock reset
pause
netsh int ip reset
pause
ipconfig /release
pause
ipconfig /renew
pause
ipconfig /flushdns
pause
--
With over 1.3 billion devices now running Windows 10, customer
satisfaction is higher than any previous version of windows.
Loading...