locked
File Watcher RRS feed

  • Question

  • Hi i'm using file watcher in my application, i've to upload a fie when ever it is created in a directory, if i run my code it is uploading when i open a new text document, even before i name and if i use the changed event,it is showing as the it is being used by some other process................... can any one help me how i can use the file watcher to upload a file when a file is created..........

    here is my code

     

     

    public void CreateWatcher()  

    {

    //Create a new FileSystemWatcher.

    FileSystemWatcher watcher = new FileSystemWatcher();

      watcher.Filter ="*.txt";//Subscribe to the Created event.

      watcher.Created +=new FileSystemEventHandler(watcher_FileCreated);

      watcher.Changed +=new FileSystemEventHandler(watcher_FileChanged);

     

    watcher.Path ="@D;/ErrorLog/";

     

    watcher.EnableRaisingEvents =true;

    }

     

     

     

    public void watcher_FileCreated(object sender, FileSystemEventArgs e)

    {

     string Filepath = string.Empty;

    Filepath = e.FullPath;

     var blob = cloudBlobContainer.GetBlobReference(Filepath);

    blob.Metadata["FileType"] = "Text";

    blob.UploadFile(Filepath);

    }

     

     

    public void watcher_FileChanged(object sender, FileSystemEventArgs e)

     string Filepath;

    Filepath = e.FullPath;

     

      var blob1 = cloudBlobContainer.GetBlobReference(Filepath);

    blob1.Metadata["FileType"] = "Text";

    blob1.UploadFile(Filepath);

    }

     

     

     

     

     
    Thursday, March 17, 2011 3:30 PM

Answers

  • In this situation I will register only Changed event with notify filter of LastWrite. In method that will be called after on changed I will try to perfom check if file is not locked with another process. Maybe I will wait for some time in miliseconds to be sure another write is not performing after the first.

    If it is not locked I will upload it to storage with retries.

    But it assumes that you will write all data in one step ;-)

    • Marked as answer by Mog Liang Thursday, March 24, 2011 8:25 AM
    Friday, March 18, 2011 12:04 PM

All replies

  • I was going to suggest that you verify you can open the file with an prior to the upload attempt, as follows:

    FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.None)

    However, this potentially introduces a race condition where the file could be opened by another process between the success of this call and the upload attempt.

    Neither does it address the larger issue that you (presumably) need to handle upload failure regardless of cause. This suggests you need to implement some retry logic on upload failure. It is probably easier to handle the general failure than handling individual failures like this. Depending on the needs of your app this could be through the built-in retry logic of the Storage Client library or through a more robust system that tracks which uploads have failed and periodically retries the uploads over a longer period than is typically used for the built-in retry logic.

    Thursday, March 17, 2011 4:28 PM
    Answerer
  • Thanks Neil, but my requirement is when ever a credit card payment is successfull, a log will be created in a folder, i've to upload that log to blob soon after that log file is created, for each and every successful transaction, a separate log file is created.
    Friday, March 18, 2011 6:18 AM
  • In this situation I will register only Changed event with notify filter of LastWrite. In method that will be called after on changed I will try to perfom check if file is not locked with another process. Maybe I will wait for some time in miliseconds to be sure another write is not performing after the first.

    If it is not locked I will upload it to storage with retries.

    But it assumes that you will write all data in one step ;-)

    • Marked as answer by Mog Liang Thursday, March 24, 2011 8:25 AM
    Friday, March 18, 2011 12:04 PM
  • Thank you, i think this approach may work for me
    Friday, March 18, 2011 12:36 PM