19. juni 2006 - 15:50Der er
4 kommentarer og 1 løsning
VB.NET Service Delete files
Hi
Jeg har lavet en service der skal delete alle filer i en folder.. Når jeg køre det fra console køre det fint... Min service installere også ok, men den deleter ikke filerne. Har prøvet at køre den som local og administrator service... Her er min lille service som køre på en timer.
Imports system.IO
Public Class Service1
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. Timer1.Enabled = True
End Sub
Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Console.WriteLine("Folder created:")
Dim Path As String = "M:\Delete\" Dim aFiles() As String = System.IO.Directory.GetFiles(Path) For Each sFile As String In aFiles ' delete the file... System.IO.File.Delete(sFile) Next
Når man laver en service må man ikke bruge form.timer men man skal gerne bruge system.timer så nu ser det sådan her ud og det køre super godt... Imports system.IO Imports System.Timers
Public Class Service1
Dim t As New System.Timers.Timer(2000)
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. AddHandler t.Elapsed, AddressOf TimerFired
t.Enabled = True
End Sub
Protected Overrides Sub OnStop() ' Add code here to perform any tear-down necessary to stop your service. t.Enabled = False End Sub
Public Sub TimerFired(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) 'Do stuff Here Dim Path As String = "C:\folder\" Dim aFiles() As String = System.IO.Directory.GetFiles(Path) For Each sFile As String In aFiles ' delete the file... System.IO.File.Delete(sFile) Next End Sub
End Class
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.