Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Create console apps that return an exit code

Total Hit ( 3338)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Writing an application that returns an ERRORLEVEL to Dos is quite difficult in VB6, because you are forced to use a Windows API that ends the application immediately, thus preventing orderly release of resources. Conversely, this task is quite simple in VB.NET, thanks to the Exit static method of the Environment class.

' Terminate the current application and return ERRORLEVEL = 1
Environment.Exit(1)

As with any regular command-line utility, you can test the errorlevel in a batch file as follows

Click here to copy the following block
@ECHO OFF
REM call the app that can set the errorlevel
MyApp
REM higher errorlevels must be tested first
IF ERRORLEVEL 3 GOTO e3
IF ERRORLEVEL 2 GOTO e2
IF ERRORLEVEL 1 GOTO e1

REM Add here code for Errorlevel = 0
REM ...
GOTO End

:e1
REM Add here code for Errorlevel = 1
REM ...
GOTO End

:e2
REM Add here code for Errorlevel = 2
REM ...
GOTO End

:e3
REM Add here code for Errorlevel = 3
REM ...

:End

UPDATE: Richard Deeming and Burton Rodman wrote us to point at an alternative, and much cleaner way, to have your VB app return an exit code to the operating system. You just have to create a Function named Main, which returns an Integer, as in:

Click here to copy the following block
Function Main() As Integer
  ' ...
  ' Return ERRORLEVEL = 1
  Return 1
End Function


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.