Hi Joseph, Following is the Explanation of FileWatcher service: File watcher Service is a windows service which continuously monitors a Windows folder for new files. When new files arrive, File watcher Service then triggers a custom action. Here we are going to create a File Watcher Service in Visual Studio. Following are the steps for creating file watcher service. 1. Start Visual Studio .NET, and create a new project. From the New Project dialog box, choose either a Visual Basic or Visual C# project,and then select windows, and select the Windows Service project type. Set the name for the project to FileWatcher. 2. Modify the file name. Once Visual Studio has created the project, you'll find a class named Service1 w ithin the project. In the Solution Explorer window, rename this file to fileWatcher.cs or FileWatcher.vb,depending on the project language. 3. Open the class(FileWatcher.cs) in Design view.Modify the service properties. In the Properties window, set the service's properties as follows: Service Class Properties Property | Value | Description | AutoLog | True | When set, automatically logs common events to the event log. In this case, since you're only going to log the OnStart event, you'll want some notation entered in the event log when you pause, stop, and resume your service | CanPauseAndContinue | True | Allows the service to be paused and continued | CanShutDown | True | Allows the service to be shut down | CanStop | True | Allows the service to be stopped | ServiceName | FileWatcher | Contains the name for the service |
|
4. Add the FileSystemWatcher component. With the FileWatcher class still open in Design view, locate the Toolbox window (press Ctrl+Alt+X, or use the View | Toolbox menu item to ensure that it's visible). Find the Components tab, and then double-click on the FileSystemWatcher component to add an instance to the designer. Modify the component's name to be fsw. 5. Set fsw's properties. Select the FileSystemWatcher component you just added and modify its properties as shwon in figure. 
Once you've selected the Add Installer link, Visual Studio .NET creates a new project file named ProjectInstaller.cs (or .vb). Right now, this new file contains two components: ServiceProcessInstaller1 and ServiceInstaller1. Note that Visual Studio .NET sets the ServiceName property of the ServiceInstaller object when you add the ServiceInstaller object to your project. If you change the name of your service class, you'll need to manually change this property to match. By default, the ServiceInstaller class's StartType property is set to Manual. If you want your service to start automatically when you install it, select the ServiceInstaller1 component in ProjectInstaller's designer, and set the StartType property to Automatic.
In order for the installer to be able to install your service, you need to supply information about the account the service will run as. For this simple service, you can set the Account property of ServiceProcessInstaller1 to be LocalSystem—that way, you needn't supply any authentication information. However, if you don't set the Account property to LocalSystem, or you don't supply a valid UserName and Password property values, the installation setup will fail. Adding FileSystemWatcher Event Code
Follow these steps to set up your code: 1. Select the FileWatcher object in the Solution Explorer window, and then use View | Designer (Shift+F7) to open the designer window. 2. Right Click on FileSystemWatcher component on the FileWatcher class designer and choose Properties. In the Properties window Choose Events and then, double click on Created Event.It will open filewatcher.cs class which will have fsw_Created function. In this function you can write any custom code. This code will automatically run whenever a new file is added to the specified folder. For example code to Execute the workflow is given below. private void fsw_Created(object sender, System.IO.FileSystemEventArgs e) { string test = "<test>m</test>"; object userid = "ProviderName::UserName"; Client cl = new Client("FieWatcherRepository", "FileWatcherWorkFlow"); cl.Execute(userid, test); cl.Close(); } Testing the Service: Unlike many applications you'll create using Visual Studio .NET, you cannot simply run a Windows service from within the development environment. You must build the executable, install it, and test it as a running service.Follow these steps to test your Windows service: 1. Use the File | Save All menu item, or press Ctrl+Shift+S, to save your entire project. 2. Choose the Build | Build menu item (or press Ctrl+Shift+B) to build your service's executable file. 3. Find the executable. Use the Visual Studio .NET Command Prompt item from the Start menu. It's installed as a subitem of the Visual Studio .NET Tools item to open a Windows command prompt. (If you don't follow these steps, you won't have your MS-DOS path set correctly.) Change to the folder where your project is stored. Navigate to the bin\debug folder for C# projects, or the bin folder for Visual Basic projects, and locate the FileWatcher.exe file you've just created. 4. Install your service. Run the InstallUtil tool that comes with Visual Studio, using the following command line: InstallUtil filewatcher.exe. 5. Start the service. Unless you modified the ServiceInstaller1 StartType property, you'll need to start your service. Bring up the Windows service manager. (The exact steps depend on your operating system. In Windows 2000, you can use the Administrative Tools applet in Control Panel and select the Services item.) In the Services tool, find the FileWatcher service and double-click it to load the properties dialog box for the service. If you want to specify a path other than C:\ for the FileWatcher class, you can do that in the Start Parameters textbox. When you're ready, press Start to start the service. 6. Investigate the Startup events. Open the Event Viewer (again, the steps vary depending on your operating system). Select the Application log, and note that you should find two entries in the event log already. The first, sent from your service's OnStart event procedure, contains the text "FileWatchService starting. There are 0 args. Watch path is 'C:\'." (The text will be different if you specified an argument when you started the service.) The second, sent automatically (because you set the service's AutoLog property to True), indicates that the service started successfully. 7. Test the service. Open Windows Explorer, navigate to the folder your service is "watching," (Open the Skelta Logger Console). Add a file to the folder. And you can see your specified workflow has been triggered. 8. Remove the service. When you're done, use the Services dialog box to stop your service. Run InstallUtil /u FileWatcherService uninstall your service. You'll also need to shut down the Windows Services applet to completely remove your service from memory.
Regards, Abhijeet Dubey |