C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Creating a hot folder window service using FileSystemWatcher.


Posted Date: 08 May 2008    Resource Type: Code Snippets    Category: Windows Services
Author: shobhit jainMember Level: Bronze    
Rating: Points: 10



Many times we need to a utility that can watch the specified folder(s) contents and can take actions accordingly.

For example we can share a directory namely "Music" on our machine. Anyone can put the music files to this folder, as soon as a music file comes to this folder, the music file is copied to the user's favorite music folder.

For this task to do we need to develop a window service. This window service will continuously watch the shared music folder with the help of

FileSystemWatcher

.

Beolw is the code to achieve the same.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.IO;

namespace FolderPollerService
{
public partial class FolderPollerSrvc : ServiceBase
{
FileSystemWatcher[] watchers;
public FolderPollerSrvc()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
// get the list of hot folders, let say from registry.
string[] sFolders = //get the folder's path
if (sFolders != null && sFolders .Length > 0)
{
watchers = new FileSystemWatcher[hotfolderInfo.Length];
for (int i = 0; i < hotfolderInfo.Length; i++)
{
watchers[i] = new FileSystemWatcher();
watchers[i].Path = sFolders [i];
watchers[i].NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
watchers[i].Filter = "*.mp3";
watchers[i].EnableRaisingEvents = true;
watchers[i].Created += new FileSystemEventHandler(FSWatcher_Created);
}
}
}

void FSWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
{
//put the file to user's favorite music folder
}

protected override void OnStop()
{
}
}
}




Responses

Author: Ben Letto    01 Jan 2009Member Level: Bronze   Points : 1
Hello Shobhit,
I'm interested in using this code...but it is not compiling for me. It has a problem with:

1) "InitializeComponent" does not exist in current context
2) "hotfolderInfo", it does not exist in current context

I'm running .NET 2.0

Thanks for your help!
Ben


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: FullScreen
Previous Resource: How we can upload files
Return to Discussion Resource Index
Post New Resource
Category: Windows Services


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use