0

Okay so I'm in a dilemma here. What I need is a solution for my following problem: I basically want to automatically upload some files to my server as soon as they got created into client system's specific folder. There will be some files that would be created in a specific path, suppose on path: C:\User\Desktop\Scanned Files, now as soon as user saves their files into this path some window service kind of thing should pick the file up and upload to my server. Process should be fast enough, I mean no longer than 1 minute.

I can either go with the windows service or scheduled task. As far as I know windows service are always running on background but they need to run with a timer which runs periodically and do the allotted operation. and scheduled task runs on a particular point of time. First of all I'm quite confused here which approach should I go with and what would fulfill my requirement more efficiently and feasibly. Or is there any better way to do this other than windows service and scheduled task?

Shilpa Soni
  • 2,034
  • 4
  • 27
  • 38

2 Answers2

4

You can make use of FileSystemWatcher class and can utilize its events in order to upload any new file in that folder to the server like below

   FileSystemWatcher folderWatcher= new FileSystemWatcher("C:\User\Desktop\Scanned Files");

   folderWatcher.EnableRaisingEvents = true;

   folderWatcher.Created += new FileSystemEventHandler(folderWatcher_Created);

   void folderWatcher_Created(object sender, FileSystemEventArgs e)
   {
        // Your code to upload the file to the server
   }
Rishi Tiwari
  • 1,041
  • 1
  • 10
  • 20
  • that seems you're asking to go with windows service option, correct me if I'm wrong here. but should I run this code in background_worker? where and how this code will be utilized? – Shilpa Soni Jun 08 '16 at 04:06
  • Okay I got it, after some research got this: http://stackoverflow.com/questions/11999335/windows-service-w-filesystemwatcher-in-c-sharp – Shilpa Soni Jun 08 '16 at 04:38
  • You can have this watcher in any type of application, it depends upon your project type. Alternatively you can create a cache dependency using this watcher and can handle the invalidate callback function of the cache dependency to upload your files. – Rishi Tiwari Jun 08 '16 at 07:51
1

You should without doubt use a Scheduled Task.

The right solution for scheduling a simple process like yours (file uploading) is the Windows Task Scheduler. It is simply better and more stable.

A service performs a specific system function to support other programs, particularly at a low level.

Services offer no scheduling, because they are designed to run all the time! On the other hand, Windows Scheduled Tasks have advanced scheduling features which can be accessed administratively. This is an advantage.

An file upload is a bad candidate for Service implementation. Even if your write it with great care and stability.

You can, however, start with a simple timer, and then migrate to a Scheduled Task.

I hope this serves you.

If you need more arguments, please Jon Galloway thoughts: See here: http://weblogs.asp.net/jongalloway//428303

Also here there is a related question: windows service vs scheduled task

Community
  • 1
  • 1
  • I would assume that Antivirus software pretty much relies on file system events, and are mostly running as Services. Why do you think FileSystemWatcher wouldn't be inapropriate? Also well writen services can run forewer without problems, also its harder, indeed. – Edgars Pivovarenoks Jun 07 '16 at 20:15
  • But I don't want to run things on a specific point of time, the approach should run all the time and keep on checking whether there is any file in my folder if yes then upload them as soon as files got created. – Shilpa Soni Jun 08 '16 at 04:08