title: Unable to move file because it's being used by another process -- my program?
body: <p>My program is unable to <code>File.Move</code> or <code>File.Delete</code> a file because it is being used "by another process", but it's actually my own program that is using it. </p>
  
  <p>I use <code>Directory.GetFiles</code> to initially get the file paths, and from there, I process the files by simply looking at their names and processing information that way. <strong>Consequently all I'm doing is working with the strings themselves, right?</strong> Afterwards, I try to move the files to a "Handled" directory. Nearly all of them will usually move, but from time to time, they simply won't because they're being used by my program. </p>
  
  <p>Why is it that most of them move but one or two stick around? Is there anything I can do to try freeing up the file? There's no streams to close. </p>
  
  <p>Edit Here's some code:</p>
  
  <pre><code>public object[] UnzipFiles(string[] zipFiles)
      {
          ArrayList al = new ArrayList(); //not sure of proper array size, so using arraylist
          string[] files = null;
  
          for (int a = 0; a &lt; zipFiles.Length; a++)
          {
              string destination = settings.GetTorrentSaveFolder() + @"\[CSL]--Temp\" + Path.GetFileNameWithoutExtension(zipFiles[a]) + @"\";
              try
              {
                  fz.ExtractZip(zipFiles[a], destination, ".torrent");
  
                  files = Directory.GetFiles(destination, 
                      "*.torrent", SearchOption.AllDirectories);
  
                  for (int b = 0; b &lt; files.Length; b++)
                      al.Add(files[b]);
              }
  
              catch(Exception e)
              {}
          }
  
          try
          {
              return al.ToArray(); //return all files of all zips
          }
          catch (Exception e)
          {
              return null;
          }
      }
  </code></pre>
  
  <p>This is called from: </p>
  
  <pre><code>try
                  {
                      object[] rawFiles = directory.UnzipFiles(zipFiles);
                      string[] files = Array.ConvertAll&lt;object, string&gt;(rawFiles, Convert.ToString);
                      if (files != null)
                      {
                          torrents = builder.Build(files);
                          xml.AddTorrents(torrents);
                          directory.MoveProcessedFiles(xml);
                          directory.MoveProcessedZipFiles();
                      }
                  }
                  catch (Exception e)
                  { }
  </code></pre>
  
  <p>Therefore, the builder builds objects of class Torrent. Then I add the objects of class Torrent into a xml file, which stores information about it, and then I try to move the processed files which uses the xml file as reference about where each file is.</p>
  
  <p>Despite it all working fine for most of the files, I'll get an IOException thrown about it being used by another process eventually here: </p>
  
  <pre><code>public void MoveProcessedZipFiles()
      {
          string[] zipFiles = Directory.GetFiles(settings.GetTorrentSaveFolder(), "*.zip", SearchOption.TopDirectoryOnly);
  
          if (!Directory.Exists(settings.GetTorrentSaveFolder() + @"\[CSL] -- Processed Zips"))
              Directory.CreateDirectory(settings.GetTorrentSaveFolder() + @"\[CSL] -- Processed Zips");
  
          for (int a = 0; a &lt; zipFiles.Length; a++)
          {
              try
              {
                  File.Move(zipFiles[a], settings.GetTorrentSaveFolder() + @"\[CSL] -- Processed Zips\" + zipFiles[a].Substring(zipFiles[a].LastIndexOf('\\') + 1));
              }
              catch (Exception e)
              {
              }
          }
      }
  </code></pre>