title: Is file being copied right now?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/12578177/c-sharp-from-time-to-time-check-if-a-file-exists-and-read-from-it">C# - from time to time check if a file exists and read from it</a>  </p>
  </blockquote>
  
  
  
  <p>I'm using file watcher to check if a file was craeted in a directory. If it was, then I want to open it and remove to another directory.</p>
  
  <p>My approach is also to use <code>FileShare.None</code> to an ensure exclusive access. What I did is:</p>
  
  <pre><code>class Program
      {
          private static void Main(string[] args)
          {
              FileSystemWatcher fileWatcher = new FileSystemWatcher();
              fileWatcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
              fileWatcher.Created += fileWatcher_Created;
              fileWatcher.EnableRaisingEvents = true;
              Console.ReadLine();
          }
  
          private static void fileWatcher_Created(object sender, FileSystemEventArgs e)
          {
              WorkOnFile(e.FullPath);   
          }
  
  
          //must be done completely. How do I ensure it?
          private static void WorkOnFile(string fileName)
          {
              using (FileStream f = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None))
              {
                  Thread.Sleep(40000); // some long operations
              }            
          }
    }
  </code></pre>
  
  <p>The bottom line is, that it necessary to do <code>WorkOnFile()</code> completely and only one time for one file. If a file being copied, then I need to call <code>WorkOnFile()</code> again until it has completely been copied and none process is using it.</p>
  
  <p>How do I reach it? </p>