473,378 Members | 1,360 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 9384
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dmitri Shvetsov | last post by:
Hi, Can I start an external process from the Web Service? I'm using a code, compiler keeps silence, compiles ok and starts the project. When I trace in Debugger it doesn't start an external...
10
by: Tony | last post by:
I am running an application called AcroComm.exe to poll time clocks here at our company. I have written a small C# app that will poll the clocks based on information found in a DB. My problem is...
1
by: Bucky Pollard | last post by:
I have a web service that needs to create a batch file and call it (since there are no APIs for the functionality I am looking for). I am using the Process and ProcessStartInfo objects. When I try...
1
by: Bill Rapoza | last post by:
We have a method that executes an arbitrary batch file (we use it for remote upgrades of our system). When we execute this method from outside of ASP.NET (either in the foreground app or from a...
0
by: Nurit N | last post by:
Hi, I have created a very simple batch file (echo hello world) and was trying to retrieve the standard output but every time I run the code it returns ExitCode as 1. The batch file runs just...
11
by: Nurit N | last post by:
This is the third newsgroup that I'm posting my problem. I'm sorry for the multiple posts but the matter becoming urgent. I hope this is the right place for it... I have created a very...
1
by: Andy | last post by:
Hello Guys: How would I be able to check if a .bat file I call had an error associated with it? I have the following code to shell out to a batch file. However: if an error occured: how would...
4
by: eva.monsen | last post by:
I'm trying to run a .BAT file using System.Diagnostics.Process. I'm having trouble getting Process.ExitCode to match up with what the .BAT file returns. Here are the contents of...
1
by: stemp1ar | last post by:
I am wondering if it possible to open a single process and run multiple commands on that process and check standard error and standard out after each command? Has anyone done something similar...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.