2 Replies - 4170 Views - Last Post: 17 February 2011 - 08:01 AM Rate Topic: -----

#1 xoops  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 81
  • Joined: 06-August 09

how to call bat file in windows service using vb.net

Posted 16 February 2011 - 08:40 PM

Hello all,

I want to question how to call bat file in windows service using vb.net. This is my code:

Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Diagnostics
Imports System.Linq
Imports System.ServiceProcess
Imports System.Text
Imports System.Timers


Namespace xlWebService

    Public Class Service1 : Inherits System.ServiceProcess.ServiceBase

        Private syncTimer As Timer = Nothing
        Private components As System.ComponentModel.Container


        Public Sub New()
            InitializeComponent()
        End Sub

        Shared Sub Main()
            Dim ServicesToRun As System.ServiceProcess.ServiceBase()

            ' More than one user Service may run within the same process. To add
            ' another service to this process, change the following line to
            ' create a second service object. For example,
            '
            '   ServicesToRun = New System.ServiceProcess.ServiceBase[] {new WinService1(), new MySecondUserService()};
            '
            ServicesToRun = New System.ServiceProcess.ServiceBase() {New Service1()}

            System.ServiceProcess.ServiceBase.Run(ServicesToRun)
        End Sub


        Private Sub InitializeComponent()
            '
            'Service1
            '
            Me.ServiceName = "xlWinService"

        End Sub

        Protected Overrides Sub onstart(ByVal args() As String)
            ' Add code here to start your service. This method should set things
            ' in motion so your service can do its work.
            syncTimer = New Timer()
            Me.syncTimer.Interval = 2000
            AddHandler Me.syncTimer.Elapsed, New System.Timers.ElapsedEventHandler(AddressOf Me.syncTimer_Tick)
            syncTimer.Enabled = True

        End Sub

        Public Function App_Path() As String
            Return System.AppDomain.CurrentDomain.BaseDirectory()
        End Function

        Protected Overrides Sub onstop()
            ' Add code here to perform any tear-down necessary to stop your service.
            syncTimer.Enabled = False
        End Sub

        Private Sub syncTimer_Tick(ByVal sender As Object, ByVal e As EventArgs)
            Dim batFile As String = App_Path() & "makeDir.bat"
            System.Diagnostics.Process.Start(batFile)
            'Shell(batFile, AppWinStyle.NormalFocus)
            'System.Diagnostics.Process.Start("notepad")
        End Sub


    End Class
End Namespace



this service created in service windows(service manager windows), but when i start this service not happening. What wrong in my code, thanks for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: how to call bat file in windows service using vb.net

#2 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 148
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: how to call bat file in windows service using vb.net

Posted 17 February 2011 - 07:04 AM

Your code looks fine, but do you get any errors?
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1057
  • View blog
  • Posts: 4,099
  • Joined: 02-July 08

Re: how to call bat file in windows service using vb.net

Posted 17 February 2011 - 08:01 AM

I would try a Threading Timer, they are the preferred timer for services.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1