Write a application to get or return an exit code
How to get an exit code from an application. Example: I call: System.Diagnostic.Start("regsvr32", "mydll.dll"); How to get an exit cod开发者_开发百科e from regsvr32?
And how to write an application which returns an exit code (like regsvr32). Thanks.
I'am using .NET 4.0 & C#.
This is actually two separate questions, but... The exit code is the value returned from the program's Main function. So:
public static int Main()
{
// code
return 0;
}
Will return an exit code of 0. Unless the preceding // code
does something different.
Use the Process' ExitCode property to get this value from the application to execute.
0 is typically returned when the program succeeds. Anything else is a failure but this is a matter of interpretation.
Something like this should be sufficient.
private int CallSomething()
{
using ( var p = new Process() )
{
p.StartInfo = new ProcessStartInfo("RegSvr32");
p.Start();
p.WaitForExit();
return p.ExitCode;
}
}
p.ExitCode
is the exit code of the called process.
Here is a helper class which shows you how to get the exit code, along with the output stream and error stream. Might be what you are looking for.
/// <summary>
/// Run an executable.
/// </summary>
/// <param name = "executablePath">Path to the executable.</param>
/// <param name = "arguments">The arguments to pass along.</param>
/// <param name = "workingDirectory">The directory to use as working directory when running the executable.</param>
/// <returns>A RunResults object which contains the output of the executable, plus runtime information.</returns>
public static RunResults RunExecutable( string executablePath, string arguments, string workingDirectory )
{
RunResults runResults = new RunResults();
if ( File.Exists( executablePath ) )
{
using ( Process proc = new Process() )
{
proc.StartInfo.FileName = executablePath;
proc.StartInfo.Arguments = arguments;
proc.StartInfo.WorkingDirectory = workingDirectory;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.OutputDataReceived +=
( o, e ) => runResults.Output.Append( e.Data ).Append( Environment.NewLine );
proc.ErrorDataReceived +=
( o, e ) => runResults.ErrorOutput.Append( e.Data ).Append( Environment.NewLine );
proc.Start();
proc.BeginOutputReadLine();
proc.BeginErrorReadLine();
proc.WaitForExit();
runResults.ExitCode = proc.ExitCode;
}
}
else
{
throw new ArgumentException( "Invalid executable path.", "executablePath" );
}
return runResults;
}
public class RunResults
{
public int ExitCode;
public StringBuilder Output = new StringBuilder();
public StringBuilder ErrorOutput = new StringBuilder();
}
In order to return an exit code yourself, just return an integer from your main() method.
You can return an integer from your Main() method within a console app, for example.
static int Main(string[] args)
{
try
{
//do some stuff
return 0; //everything is good
}
catch //you will want more specific error-handling, catch-all is for example only
{
return 1; //something blew up!
}
}
Use the Process.ExitCode property
Looks like System.Diagnostic.Start
method returns a System.Diagnostic.Process
class. This class has an ExitCode
Property which you should be able to use to get the return value.
精彩评论