2010-08-10 6 views
3

.NET 3.5에 작은 Windows 서비스를 작성하려고합니다.이 파일은 "C : \ demofolder"의 새 파일 인 경우 10mn마다 확인한 다음 전자 메일을 보냅니다.Windows 서비스

이 하위 홈페이지 '보다 더 선언 : 지금까지 내가 다음 코드처럼 여기까지 만든 다음, 공공 하위 새로운 기능 오류()

Imports System 
Imports System.Timers 
Imports System.ServiceProcess 
Public Class TestMyService 

    ' A timer that controls how frequenty the example writes to the 
    ' event log. 
    Private serviceTimer As Timer 

    Public Sub New() 

     ' Set the ServiceBase.ServiceName property. 
     ServiceName = "TestMyService Service" 

     ' Configure the level of control available on the service. 
     CanStop = True 
     CanPauseAndContinue = True 
     CanHandleSessionChangeEvent = True 

     ' Configure the service to log important events to the 
     ' Application event log automatically. 
     AutoLog = True 

    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. 
    End Sub 

    Protected Overrides Sub OnStop() 
     ' Add code here to perform any tear-down necessary to stop your service. 
    End Sub 

Public Shared Sub Main() 

    ' Create an instance of the TestMyService class that will write 
    ' an entry to the Application event log. Pass the object to the 
    ' shared ServiceBase.Run method. 
    ServiceBase.Run(New TestMyService) 

End Sub 


End Class 

나는 다음과 같은 오류 메시지가있어이있다 mcWinService.TestMyService.Main(), mcWinService.TestMyService.Main()

공용 공유 Sub Main() '에는 동일한 서명이있는 여러 정의가 있습니다.

디자이너에서 생성 한 Public Sub New() '형식'mcWinService.TestMyService '는 InitializeComponent 메서드를 호출해야합니다.

답변

6

시도

ServiceBase.Run (뉴 TestMyService) 공중에서 공유 하위 홈페이지를 이동()

보호 오버라이드 (override) 하위 ONSTART

Public Shared Sub Main()을 제거하십시오.

또한 Public Sub New()는 "속성 창"에서 해당 속성을 설정할 수 있으므로 제거하십시오. F7을 눌러 코드에서 디자이너보기로 전환합니다.

업데이트 1 : 폴더에 대한 Windows 서비스의 예는 더 관련 독서 확인을 위해

Imports System.Net.Mail 
Imports System.Net 
Imports System.Timers 
Imports System.IO 

Public Class DemoFolderMonitor 

    Private Shared timer As System.Timers.Timer 
    Private Shared timerInterval As Integer 

    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. 

     ' Use the EventLog object automatically configured by the 
     ' ServiceBase class to write to the event log. 
     'EventLog.WriteEntry(String.Format("DemoFolderMonitor Service starting.")) 

     ' Set the Interval (1sec = 1000 milliseconds). 
     timerInterval = 2000 
     timer = New System.Timers.Timer(timerInterval) 
     EventLog.WriteEntry("DemoFolderMonitor Service starting.") 

     ' Hook up the Elapsed event for the timer. 
     AddHandler timer.Elapsed, AddressOf WatchFolder 

     timer.Interval = timerInterval 
     timer.Enabled = True 

    End Sub 

    Protected Overrides Sub OnStop() 
     ' Add code here to perform any tear-down necessary to stop your service. 
     EventLog.WriteEntry("DemoFolderMonitor Service stopping...") 

    End Sub 

    Protected Sub WatchFolder() 

     Dim watcher As New FileSystemWatcher 
     watcher.Path = "C:\Demo\" 

     watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName) 

     'watch all file types. 
     watcher.Filter = "*.*" 
     ' Add event handlers. 
     AddHandler watcher.Changed, AddressOf OnChanged 
     AddHandler watcher.Created, AddressOf OnChanged 
     AddHandler watcher.Deleted, AddressOf OnChanged 
     AddHandler watcher.Renamed, AddressOf OnRenamed 

     ' Begin watching. 
     watcher.EnableRaisingEvents = True 

    End Sub 

    'Define the event handlers. 
    Private Shared Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs) 
     ' Specify what is done when a file is changed, created, or deleted. 
     Dim changeLog = "File: " & e.FullPath & " " & " | Action: " & e.ChangeType.ToString 
     WriteChangeLog(changeLog) 

    End Sub 

    Private Shared Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs) 
     ' Specify what is done when a file is renamed. 
     Dim changeLog = "File" & e.OldFullPath & " " & "renamed to" & " " & e.FullPath 
     WriteChangeLog(changeLog) 

    End Sub 

    'Custom action. You can either send an e-mail or write change to local file 
    'In this example I write change to local file 
    Private Shared Sub WriteChangeLog(ByVal changeLog As String) 

     'Create a text file and write the change log to the text file 
     Dim filename As String = DateTime.Now.ToString("hh-mm-ss") & ".txt" 
     Dim writer As StreamWriter 
     writer = File.CreateText("C:\ChangeLog\" & filename) 
     writer.WriteLine("Datetime Log at {0} Log Message: {1}", DateTime.Now.ToString("MMM-dd-yyyy @ hh:mm:ss tt"), changeLog) 
     writer.Close() 

    End Sub 

End Class 

을 모니터링 :

응원!

+0

샘플 및 링크를 제공해 주셔서 감사합니다. – Narazana

2

다른 프로젝트 파일에 다른 Main 메서드가있는 것 같습니다. 프로젝트에 대한 광범위한 검색을 수행하고 찾을 수 있는지 확인하십시오.

프로젝트의 등록 정보 대화 상자로 가서 Sub Main이 아닌 시작 객체로 TestMyService을 선택하여 오류를 수정할 수도 있습니다.

+0

Service1.designer.vb에 Shared Sub Main()이 있습니다. 자동 생성 된 것 같습니다. 시작 개체로 TestMyService를 선택했습니다. 아직 오류가 있습니다. – Narazana

+0

@ 모론 : 서비스를 추가 할 때 이해가되지 않습니다. 이 프로젝트를 위해 이미 많은 코드를 작성하지 않았다면 처음부터 새로 시작하거나 새로운 서비스 프로젝트를 만드는 것이 가장 쉽습니다.이 기사는 취해야 할 단계를 보여줍니다. http://www.codeguru.com/vb/gen/vb_system/services/article.php/c4825 –

+0

새로운 프로젝트입니다. 나는 Windows 서비스를 작성하는 법을 배우기 위해 4 천만 전에 그것을 만들었습니다. – Narazana