title: C# - StreamReader/StreamWriter - File used by another process
body: <p>First off, I made sure that I dispose and close everything (both reader and writer) properly. The method I'm doing is that I'm using the using statement; and before this, I manually used the dispose and close method provided by both the writer and reader. Both of these methods won't solve the problem.
  Secondly, also the point that I'm suspecting, I'm processing a lot of files both reading and writing. Further, every time I run the program, the program will run deeper into the list of the files that are supposed to be processed (ie, I have 10 files, the first time I run the program it will process the first 2 before throwing the error; the second time I run the program it will process the first 5 before throwing the error and so on).
  So how am I supposed fix this problem/bug?
  Thanks in advance!</p>
  
  <p>Edit - Code attached</p>
  
  <pre><code>    public static void FileProcessing(string initPath, string targetPath, string startLine)
      {
          string line = null;
          string content = null;
          string nl = "\r\n";
          using (StreamReader reader = new StreamReader(initPath))
          {
              while (!reader.ReadLine().Contains(startLine)) ;
  
              while (!reader.EndOfStream)
              {
                  line = reader.ReadLine();
                  // Ignore empty line.
                  if (line.Length &gt; 0)
                  {
                      if (!line.Contains("&lt;div"))
                          content += line += nl;
                      else
                          break;
                  }
              }
          }
  
          using (StreamWriter writer = new StreamWriter(targetPath))
          {
              writer.Write(content.Trim());
          }
      }
  
      /// &lt;summary&gt;
      /// Create a temporary text file that correspond to the calendar department page
      /// and return the path.
      /// &lt;/summary&gt;
      public static string CreateFile(string path, string title)
      {
          string npath = path + title + ".txt";
          if (!File.Exists(npath))
              File.CreateText(npath);
          return npath;
      }
  
      /// &lt;summary&gt;
      /// Return the title of the page.
      /// &lt;/summary&gt;
      /// &lt;param name="path"&gt;&lt;/param&gt;
      public static string GetTitle(string path)
      {
          string line;
          using (StreamReader reader = new StreamReader(path))
          {
              for (int i = 0; i &lt; 31; ++i)
              {
                  line = reader.ReadLine();
                  // Ignore empty line.
                  if (line.Length &gt; 0)
                  {
                      if (line.Contains("&lt;h1&gt;"))
                      {
                          return line.Slice(4, -5);
                      }
                  }
              }
              return "null";
          }
      }
  
  
      /// &lt;summary&gt;
      /// Get content from temp path and
      /// output the processed text to destination path.
      /// &lt;/summary&gt;
      /// &lt;param name="tempPath"&gt;&lt;/param&gt;
      /// &lt;param name="title"&gt;&lt;/param&gt;
      public static void WriteProcessedText(string tempPath, string targetPath)
      {
          string content = null;
          using (StreamReader reader = new StreamReader(tempPath))
          {
              string nl = "\r\n";
              string line = null;
              while (!reader.EndOfStream)
              {
                  line = HtmlRemoval.StripTagsRegex(reader.ReadLine()) + nl;
                  if (line.Contains("&amp;nbsp;"))
                      line = line.Replace("&amp;nbsp;", " ");
                  content += line;
              }
          }
  
          using (StreamWriter writer = new StreamWriter(targetPath))
          {
              writer.Write(content.Trim());
          }
      }
  </code></pre>