Windows services ...need some help

Hi guys ,

I recently figured out the way to create a simple windows service using c# ...iam required to do the following ,...

I need to keep monitoring a particualr directory on our ftp site ..every few minutes ...right now if any of u can help me out in doing it on any of my local directories it shopuld be fine....

I need to keep monitoring this particular Directory every few min ..if any new files are added to it ...I need to downalod this paricular new file frm the Dir and put it in myy local folder ....( for now I wouldnt mind the code for just copyin the files frm my local dir which i would be monitoring to some other dir in my local system) ...

I need to know how to make the service run every few min ...i mean how to make it run for a given time frame ...


Hope u guys can help

Cheers
Ashwin


Ashwin_shastryAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DaHaCommented:
Is this local FTP site local to the machine you are doing this work from? Is there anyway you can map a folder to it? If so... this job can be made very easy.

Rather than monitor every few minutes, you could very easily just use the FileSystemMonitor class (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiofilesystemwatcherclasstopic.asp) to keep an eye out for new files within the directory you specify, waiting for the Created event to be fired, when that happens, you simply carry out your logic within your event handler to copy/move the new file to the appropriate location, such code would be as simple as…

   
   FileSystemWatcher fileWatcher;
   fileWatcher = new FileSystemWatcher(@"C:\FolderToWatch\");
   fileWatcher.Created+=new FileSystemEventHandler(fileWatcher_Created);
   fileWatcher.EnableRaisingEvents = true;

   private void fileWatcher_Created(object sender, FileSystemEventArgs e)
   {
      File.Copy(e.FullPath, @"C:\DestinationDirectory\" + e.Name);
      //or
      File.Move(e.FullPath, @"C:\DestinationDirectory\" + e.Name);
   }

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chinmay PatelChief Technology NinjaCommented:
Hi There,

Check out this:
http://spaces.msn.com/members/nasha/?partqs=cat%3DWindows+Services&_c11_blogpart_blogpart=blogview&_c=blogpart

There are 5 parts, start from very first part to get complete understanding about Windows Services.

Regards,
Chinmay
Ashwin_shastryAuthor Commented:
Thanks DaHa and chinmay,

Daha ..ur code works perferct ..thanks a lot and Chinmay ...ur link provided me a beautiful insight into services ...I would like to know if there is any way by which I can reuse the same code to movies files from remote locations ..What i mean bythis is ..I would have to monitor a directory at a remote site ...if there are any changes ..I would want to download that particular file...

I will increase  the points ..hope u guys can help me

cheers
Ashwin
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

DaHaCommented:
It all depends on how you are able to access the remote servers.

One of the easiest ways (IMO) is simply to map a drive letter to the location... such as mapping \\10.20.30.40\someshare\ to the Z: drive on the monitoring system.

If the only mechanism you can use to access it is via FTP for instance... then the above solution would not work as File.Copy() only works on things that it sees as local (even if it is a mapped network share), and in such a case you would need to have an FTP client doing the actual monitoring and interfacing.
Chinmay PatelChief Technology NinjaCommented:
Hi There,

I would go with DaHa but its also possible though not easy by other means, and that is by
setting a client on remote system which LISTENS to your commands. if REMOTE means
a computer outside of your network where u can't VPN into than you'll need your client
running on that system otherwise the DaHa way is easy to implement.

Regards,
Chinmay
davidlars99Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.