Monday, April 16, 2012 12:19 PM
I have a local service which is running on my system. I want to delete some files or folders from my system(may be file is present in D:) through that service. Is it possible? anybody, please give me a piece of code?
Monday, April 16, 2012 1:25 PM
check below sample
Tuesday, April 17, 2012 4:48 AM
First we will understand how to monitor a folder for changes using FileSystemWatcher class, which is provided by .NET library. Then we will convert this program to a Windows Service to run in the background. We will also discuss about how to install service, start and stop it.
Make use of FileSystemWatcher Class -
- Import System.IO namespace
- Create an object of FileSytemWatcher class
- Set property of FileSystemWatcher object like Path and IncludeSubDirectories.
- Associated events like Created, Deleted etc. with event handlers using AddHandler statement in VB.NET or use += operator in case of C#.
- Enable monitoring by setting EnableRaisingEvents to true to start firing events when changes take place in the given path.
The following program shows how to display a message when a file is created or deleted from d:\ folder.
- Create a new Console Application using Visual Studio.NET 2005
- Create a class with the name FileSytemMonitor and place the following code in it.
Imports System.IO Public Class FileSystemMonitor Public Shared Sub Main() Dim fsw As New FileSystemWatcher() ' create an object of FileSystemWatcher ' set properties of FileSystemWatcher object fsw.Path = "d:\" fsw.IncludeSubdirectories = True ' add event handlers AddHandler fsw.Created, New FileSystemEventHandler(AddressOf File_Created) AddHandler fsw.Deleted, New FileSystemEventHandler(AddressOf File_Deleted) fsw.EnableRaisingEvents = True ' enable monitoring Console.WriteLine("Started Monitoring d:\ folder. Press enter key to stop.") Console.ReadLine() End Sub ' event handler to handle created event Public Shared Sub File_Created(ByVal obj As Object, ByVal e As FileSystemEventArgs) Console.WriteLine(e.FullPath & " - Created") End Sub ' event handler to handle deleted event Public Shared Sub File_Deleted(ByVal obj As Object, ByVal e As FileSystemEventArgs) Console.WriteLine(e.FullPath & " - Deleted") End Sub End Class
- Change start up object of the project to FileSystemMonitor class.
- Run the project using F5.
- Make some changes to d:\ folder and see what messages are displayed in your console.
- Press Enter key to stop monitoring
Creating a Windows Service To Monitor File System –
Now let us build a Windows Service to monitor file system. A Windows Service is program that runs in the background and perform its operations without any user interaction. Our file monitoring application is a good candidate for Windows service.
A windows service can be installed, uninstalled using a utility - InstallUtil.Exe - provided by .NET.
Once a windows service is installed, it can be managed - stared, stopped etc. - using Services program provided by Windows.
Let us now take steps to create a windows service to monitor a folder provided by user at the time of configuring service. If user doesn't provide folder name then we use d:\ as the default folder. We write a line of text with date , time , filename and operation in a text file with the name FileSystem.Log that is placed in temp directory of Windows OS
- Start Visual Studio.NET 2005
- Select File->New Project
- Expand Visual Basic node and select Windows as Project type.
- Select Windows Service as the template.
- Enter FSMonitor as the project name and select the folder where you want to place the project.
- You get Service1.vb[Design].
- Click on link click here to switch to code view to get into code view - service1.vb.
- Modify the code as follows
Imports System.IO Public Class Service1 Dim fsw As FileSystemWatcher Dim fw As StreamWriter Protected Overrides Sub OnStart(ByVal args() As String) fw = New StreamWriter(Environment.GetEnvironmentVariable("temp") & "\filesystem.log") fsw = New FileSystemWatcher() If args.Length > 0 Then fsw.Path = args(0) fsw.IncludeSubdirectories = True Else ' default settings fsw.Path = "d:\" fsw.IncludeSubdirectories = False End If AddHandler fsw.Created, New FileSystemEventHandler(AddressOf File_Created) AddHandler fsw.Deleted, New FileSystemEventHandler(AddressOf File_Deleted) fsw.EnableRaisingEvents = True End Sub Public Sub File_Created(ByVal obj As Object, ByVal e As FileSystemEventArgs) fw.WriteLine(Now.ToShortDateString & " " & Now.ToShortTimeString & " - " & e.FullPath & " - Created") End Sub Public Sub File_Deleted(ByVal obj As Object, ByVal e As FileSystemEventArgs) fw.WriteLine(Now.ToShortDateString & " " & Now.ToShortTimeString & " - " & e.FullPath & " - Deleted") End Sub Protected Overrides Sub OnStop() fw.Close() End Sub End Class
- Go to Designer, right click and select Add Installer option from popup menu.
- Visual Studio creates two components - ServiceProcessInstaller1 and ServiceInstaller1
- Select ServiceInstaller1 and change following properties
- ServiceName : FSMonitor
- DispalyName : File System Monitor
- StartType : Manual (default)
- Select ServiceProcessInstaller1 and change following properties
- Account : LocalSystem
- Go to solution explorer and build the project to create .exe (FSMonitor.exe) file for the project
- Go to command prompt using Microsoft Visual Stdio.2005 -> Visual Studio Tools -> Visual Studio.NET 2005 Command Prompt
- Go to the directory where FSMonitor.exe is present ( debug directory under bin directory)
- Enter the following command to install service
- Installutil FSMonitor.exe
Tuesday, April 17, 2012 5:55 AM
Is it an .asmx web service or windows service?
If it is .asmx web service, you can try what AT and vanya suggested. In addition to that make sure, to give the IIS_IUSRS group Modify permissions to the directory where you are deleting files.