by jask2002
18. October 2012 01:50
Main() method in C# can have either (void or int) as the return value
Void:
namespace TestSolution
{
class Program
{
static void Main(string[] args)
{
}
}
}
int:
namespace TestSolution
{
class Program
{
static int Main(string[] args)
{
return 1001;
}
}
}
At this point the question arises in my mind is how to read the return value from the Main() when called from any script VBscript or batch file or PowerShell?
Here is the solution
VbScript
iRetVal = 0
SPath = "\Scripts\TestSolution.exe"
iRetVal = oShell.Run (sPath,0,TRUE)
msgbox iRetVal
If iRetVal = 1001 then
' DO something
Else
' Do something else
End if
Batch File
rem test.bat
@echo off
TestSolution
@if "%ERRORLEVEL%" == "1001" goto good
:good
echo Execution Succeded
echo return value = %ERRORLEVEL%
goto end
:end
PowerShell
PS C:\Users\jaskis\Documents\Visual Studio 2010\Projects\TestSolution\TestSolution\bin\Debug> .\TestSolution.exe
PS C:\Users\jaskis\Documents\Visual Studio 2010\Projects\TestSolution\TestSolution\bin\Debug> $lastexitcode
1001
|
Has this post helped you? Saved you? If you'd like to show your appreciation. Please buy me a coffee or make a small contribution
toward blog's maintenance(to keep it Ads free )
|
2460eb66-58dd-401f-a53e-fc8c20b87031|0|.0
Tags: .Net, PowerShell, VbScript, Main
.Net | C#