简体   繁体   中英

Is it possible to create a C# Windows Service that detects new file creation on Windows XP?

I have a folder that will contain a temporary file that will be created by web service. I want the file to be converted by .exe program I have via command line. Now, I have already generated web service that will create file and store in one specific folder.

Here is the scenario:

  1. Web service receive amr byte data from my mobile app.
  2. Web service deletes all files existing in the folder. (to remove .amr file and .mp3 file that was generated from the previous round)
  3. Web service create new .amr file in the folder.
  4. Windows Service detects that there is a new file created. It call converter.exe. Once complete, it creates .mp3 file in the same folder.

Now, I have no problem with step1-3. The problem is step4, which I need to convert .amr file to .mp3 file. In order to do this, I need to pass some dos-command to converter.exe. I found that it is impossible for me to do it from my web service, so I am thinking of creating C# Windows Service that will watch the folder storing the files. Is it possible to create Windows Service that is able to detect new file creation and run converter.exe as soon as a new file is created ? I did search on the Internet and found that I may be create Windows Service with FileSystemWatcher, but i am just not sure if it will be possible.

The reason I found when trying to execute converter.exe from web service is about permission. I did everything but still can't make it success.(Using Web Service, converter.exe is shown on the process list in task manager as local system, but it will not be executed because of some security reason...) Hope there will be no security problem when I try to execute converter.exe using Windows Service.

Thanks in advance for all answers and suggestions :)

System.IO.FileSystemWatcher will do the trick. The Created event will tell you when a file has been created in the directory you're watching. To start your conversion program, take a look at the System.Diagnostics.Process.Start method. Both of these should work fine in a Windows service.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM