Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,264 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,231 people online right now. Registration is fast and FREE... Join Now!




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
10 Sep, 2007 - 05: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
+Quote Post


PsychoCoder
RE: How To Call/execute Batch File From .Net Windows Service
10 Sep, 2007 - 03:41 PM
Post #2

loves.Coding(this);
Group Icon

Joined: 26 Jul, 2007
Posts: 12,281



Thanked: 372 times
Dream Kudos: 10775
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

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 offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 12:20PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month