title: File being used by another process, cannot be accessed
body: <p>I am creating loggers to log messages to a text file. The function DeleteOldLogs() is responsible to delete any logs that are over 14 days old.
  When I run TestMethod1(), I receive an error "The process cannot access the file because it is being used by another process"
  Is there a way to avoid that error?</p>
  
  <p>// TestClass</p>
  
  <pre><code>[TestMethod]
  public void TestMethod1()
  {
      LoggerViewModel lv = new LoggerViewModel();
      lv.DeleteOldLogs();
  }
  </code></pre>
  
  <p>// LoggerViewModel class.</p>
  
  <pre><code>[Export]
      [PartCreationPolicy(System.ComponentModel.Composition.CreationPolicy.Shared)]
      public class LoggerViewModel : ViewModelBase
      {
          public ObservableCollection&lt;LogItem&gt; LogItems { get; set; }
          public ObservableCollection&lt;LogItem&gt; logItemCollection = new ObservableCollection&lt;LogItem&gt;();
          protected static readonly ILog log = LogManager.GetLogger(typeof(LoggerViewModel));
          private string dirPath = @"C:\LogFolder";
  
          [ImportingConstructor]
          public LoggerViewModel()
          {
              MemoryAppender memoryAppender = new MemoryAppender();
  
              BasicConfigurator.Configure(memoryAppender);
              Log();
              var events = memoryAppender.GetEvents();
              foreach (LoggingEvent loggingEvent in events)
              {
                  LogItem logItem = new LogItem(loggingEvent.TimeStamp, loggingEvent.Level, loggingEvent.RenderedMessage);
                  logItemCollection.Add(logItem);
              }
              LogItems = logItemCollection;
          }
  
          public void Log()
          {
              string stackTrace = Environment.StackTrace;
              string[] array = stackTrace.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
              foreach (string item in array)
              {
                  log.Debug(item);
              }
          }
  
          public void DeleteOldLogs()
          {
              List&lt;string&gt; delPaths = new List&lt;string&gt;();
              foreach(var file in Directory.GetFiles(dirPath))
              {
                  FileInfo info = new FileInfo(file);
                  DateTime fileCreationDate = info.CreationTime;
                  DateTime twoWeeksAgo = DateTime.Today.AddDays(-14);
                  if (fileCreationDate &lt; twoWeeksAgo)
                  {
                      string delPath = dirPath + @"\" + info.Name;
                      delPaths.Add(delPath);
                  }
              }
              foreach(string dp in delPaths)
              {
                  FileSystem.DeleteFile(dp, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);
              }
          }
      }
  </code></pre>