title: How to avoid Filewatcher lock file during System.IO.File.Copy
body: <p>I had developed a filewatcher program to monitor a folder, if there are any changed of the file, it will copy the file to another folder.</p>
  
  <p>But I found that there will be error message when writing the original file (e.g. file being prcoess by another application...) it seems that the file locked when running [System.IO.File.Copy] copying to another folder.</p>
  
  <p>Is there any solution can avoid the original file locked by the filewatcher/System.IO.File.Copy? Thanks.</p>
  
  <p>The following is my code:</p>
  
  <pre><code>    private void fileWatcher_Changed(object sender, System.IO.FileSystemEventArgs e)
      {
          DateTime lastWriteTime = File.GetLastWriteTime(e.FullPath); 
  
          if (lastWriteTime != lastRead)
          {
  
  
              txtLog.Text += e.ChangeType + ": " + e.FullPath + "\r\n";
              txtLog.Focus();
              txtLog.Select(txtLog.TextLength, 0);
              txtLog.ScrollToCaret();
  
              try
              {
                  string myPath = e.FullPath;
                  string myFile = e.Name;
  
                  System.IO.FileInfo myFileInfo = new System.IO.FileInfo(myFile);
  
                  string myAttibs = myFileInfo.Attributes.ToString();
  
                  System.IO.File.Copy(myPath, @"D:\\Folder\\Output\\" + myFile, true);
  
                  lastRead = lastWriteTime; 
  
              }
              catch (System.IO.IOException ex)
              {
                  System.IO.IOException myex = ex;
              }
              catch (System.Exception ex)
              {
                  System.Exception myex = ex;
              }
  
          }
      }
  </code></pre>