Cant we use FileSystemWatcher in Windows Services? Error "Not of a valid form"  
Author Message
srishu





PostPosted: Visual Basic General, Cant we use FileSystemWatcher in Windows Services? Error "Not of a valid form" Top

Hi,

I have added a FileSystemWatcher to my Service program and build and installed it successfully. But when i start the service it says,

Error. "Not of a valid form"

Can this object be used only on a valid form Or am I making any stupid mistake

This is the code I have written.

Public Class FlashTrapperService

Dim WithEvents fswF****ch As System.IO.FileSystemWatcher

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.

fswF****ch = New System.IO.FileSystemWatcher()

username = My.User.Name

username = username.Remove(0, username.LastIndexOf("\") + 1)

If My.Computer.FileSystem.DirectoryExists("C:\Documents and Settings\" & username & "\Local Settings\Temporary Internet Files") Then

folderpath = "C:\Documents and Settings\" & username & "\Local Settings\Temporary Internet Files"

ElseIf My.Computer.FileSystem.DirectoryExists("D:\Documents and Settings\" & username & "\Local Settings\Temporary Internet Files") Then

folderpath = "D:\Documents and Settings\" & username & "\Local Settings\Temporary Internet Files"

End If

fswF****ch.Path = folderpath

fswF****ch.NotifyFilter = (IO.NotifyFilters.LastAccess Or IO.NotifyFilters.LastWrite Or IO.NotifyFilters.FileName Or IO.NotifyFilters.DirectoryName)

fswF****ch.Filter = "*.swf"

AddHandler fswF****ch.Changed, AddressOf fswF****ch_Changed

AddHandler fswF****ch.Created, AddressOf fswF****ch_Changed

fswF****ch.EnableRaisingEvents = True

fswF****ch.IncludeSubdirectories = True

End Sub

Protected Overrides Sub OnStop()

' Add code here to perform any tear-down necessary to stop your service.

fswF****ch.EnableRaisingEvents = False

End Sub

Private Sub fswF****ch_Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)

flashfolder = "D:\TrappedFlashFiles\"

filename = e.FullPath

filename = filename.Remove(0, filename.LastIndexOf("\") + 1)

filename = flashfolder & filename

If e.ChangeType = IO.WatcherChangeTypes.Changed Then

My.Computer.FileSystem.CopyFile(e.FullPath, filename, True)

End If

End Sub

Public Sub New()

' This call is required by the Windows Form Designer.

InitializeComponent()

' Add any initialization after the InitializeComponent() call.

End Sub

End Class

Thanks for your help.

Regards




Visual Basic1  
 
 
Lau Han Ching





PostPosted: Visual Basic General, Cant we use FileSystemWatcher in Windows Services? Error "Not of a valid form" Top

you can found 1 sample code inside the .net sdk.
 
 
nobugz





PostPosted: Visual Basic General, Cant we use FileSystemWatcher in Windows Services? Error "Not of a valid form" Top

Remove the call to InitializeComponent(). That's used in form constructors only.



 
 
jwooley





PostPosted: Visual Basic General, Cant we use FileSystemWatcher in Windows Services? Error "Not of a valid form" Top

It appears that you are trying to trap the user from downloading shockwave files. I'm not sure it will work based on what you supplied. In your example you are trying to pull the currently logged in user when the service starts. However, the service runs under a different principal, either one named in the service manager or a local system account. Thus the value you are getting for username will likely not be the one you are wanting. Windows services are designed to work in the background regardless of the currently logged in user. I recommend you change your segment of code that assigns the filepath variable.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx



 
 
srishu





PostPosted: Visual Basic General, Cant we use FileSystemWatcher in Windows Services? Error "Not of a valid form" Top

Hi,

Thanks for your suggestion but I tried debugging my code by removing one line and checking.

It did not work even if i removed the username part.

Actually the error or exception occcured at this line

fswF****ch.enablerisingevents=true

Regards