Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 98,765 VB.NET Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 1,046 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a VB.NET Expert

Register to Make This Box Go Away!


How to call/execute Batch File from .Net Windows Service

 
Reply to this topicStart new topic

How to call/execute Batch File from .Net Windows Service

Sim_270
post 10 Sep, 2007 - 06:16 AM
Post #1


New D.I.C Head

*
Joined: 27 Aug, 2007
Posts: 8


My Contributions


I Have one batch file.I want to run that batch file once the System is started .How can I call/execute Batch File from .Net Windows Service?
User is offlineProfile CardPM

Go to the top of the page


PsychoCoder
post 10 Sep, 2007 - 04:41 PM
Post #2


using Coding.God;

Group Icon
Joined: 26 Jul, 2007
Posts: 6,569



Thanked 24 times

Dream Kudos: 7350

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#

My Contributions


Put this function in your Windows Service (I take it you created the service and have access to its code), then call it when the service starts. In this function theres a line that reads batchExecute.Start("Path to your batch file here"), replace "Path to your batch file here" with the actual path to your batch file.

In this function it will execute your batch file (without showing a command window), then check its ExitCode and return a 0 (for successful) or 1 (for failed)

CODE

Public Shared Function ProcessBatchFile() As Integer
       'Create a variable to hold the value you wish
       'to return to your calling method
        Dim batchSuccessful As Integer
        'Create a new Process(), I just happen
       'to name this "YourName"
       Dim batchExecute As New Process()

        'This is a method that you can use to set
        'properties for your new Process()
         Dim batchExecuteInfo As New ProcessStartInfo()

       'Tell the program to not use the built-in Shell provided
       'with the OS, wew ant to start it ourselved
       batchExecuteInfo.UseShellExecute = False

       'Tell the process to not open a Console window
       batchExecuteInfo.CreateNoWindow = True

        'Set your process info here
         batchExecute.StartInfo = batchExecuteInfo
        'Start your Process(), this will start the
        'command line and send your command
        batchExecute.Start("Path to your batch file here")

        'Set the period of time to wait for the associated
        'process to exit, and blocks the current thread
        'until the time has elapsed or the
        'process has exited
        batchExecute.WaitForExit(15000)

        'Create a variable and assign it the value
        'of your processes exit code
        Dim Exit As Integer = batchExecute.ExitCode

        'Check if the process has completed
        'and return a status of 0 (succeeded). If not
        'then we want to kill the process
       If Exit > 0 And Not batchExecute.HasExited Then
            'Here you can assign a value to a variable
            'to return to your calling method
            batchSuccessful = 1
            batchExecute.Kill
       Else
              'Do something here and
               'assign your successful variable a 0
               batchSuccessful = 0
       End If
       'Return the value (1 or 0) to
       'your calling method
       Return batchSuccessful
End Function


Hope this helps smile.gif
User is online!Profile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 7/20/08 02:44PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->