title: C# File - Read files from desktop and write them to a specific file
body: <p>I created a <code>System.Timers.Timer</code> object with an interval of 5000 ms. On the <code>Elapsed</code> event of this timer, I'm searching the new PDF files which appeared on Desktop. If there are new PDF files, I add those to the specific file, but my program catch this error: <strong>The process cannot acces the file 'C:\Users\Admin\Desktop\StartupFiles.dat' because it is being used by another process</strong>.
  Here is my code:</p>
  
  <pre><code>    private readonly string fileName = Application.StartupPath + @"\StartupFiles.dat";
      private readonly string sourceDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); 
  
      void timerCheck_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
      {
          try
          {                
              if (!File.Exists(fileName))
                  File.Create(fileName);
  
              string[] PDFiles = Directory.GetFiles(sourceDirectory, "*.pdf", SearchOption.TopDirectoryOnly);
              string[] textFile = File.ReadAllLines(fileName);
  
              bool exist;
              string addText = string.Empty;
  
              foreach (string s in PDFiles) // Check the files from the desktop with the files from the fileName variabile folder
              {
                  exist = false;
                  foreach (string c in textFile)
                  {
                      if (string.Compare(s, c) == 0)
                      {
                          exist = true;
                          break;
                      }
                  }
                  if (!exist)
                  {
                      addText += s + '\n';                        
                  }
              }
              if (!string.IsNullOrEmpty(addText)) // If a new PDF appeard on the desktop, save it to file
              {
                  using (StreamWriter sw = File.AppendText(fileName))
                  {
                      sw.Write(addText);
                  }    
              }
          }
          catch (Exception ex)
          {
              MessageBox.Show(ex.Message);
          }
      }
  </code></pre>
  
  <p>Maybe I have to set a little delay between <code>ReadAllLines</code> and <code>File.AppendText</code> ?</p>