title: StreamWriter not appending to file, instead creating a new file in different directory
body: <p>I am attempting to write from one file to a temp file after making certain changes to some of the data in the source file. This was working until I extracted this logic into its own function.</p>
  
  <p>After running some tests I noticed that only the first line was being saved to the file in the location I specified. After debugging I was able to see from the second call to this function and onward the file location is in a completely different location. </p>
  
  <p>I have two questions, first - Why is this happening? Second - What is the best way to make sure this gets fixed and does not happen again?</p>
  
  <p>str[] is the file broken up by the delimiter one line at a time.
  filename is the name of the specific file contained in the directory of files (this program loops through a number of files).</p>
  
  <pre><code>public static void writeFile(string[] str, string filename)
  {
      rowCount++;
      using (StreamWriter sw = new StreamWriter(filename.Substring(0, filename.Length - 4) + "_tmp.txt", true, System.Text.Encoding.Unicode))
      {
          string tempString = String.Empty;
          for (int i = 0; i &lt; str.Length; i++)
          {
              if (isMRT)
              {
                  tempString += str[i] + ",";
              }
              else
              {
                  tempString += str[i] + "|";
              }
          }
          logger.LogMessage("Writting line to file: " + "Row #: " + rowCount + " ,File Path: " + filename.Substring(0, filename.Length - 4) + "_tmp.txt");
          sw.WriteLine(tempString);
      }
  }
  </code></pre>
  
  <p>edit:
  This is my directory and list of files and the function that calls the write method</p>
  
  <pre><code>static string directory = ConfigurationManager.AppSettings["Directory"];
  static List&lt;String&gt; fileNames = Directory.GetFiles(directory).ToList();
  
  private static void activityCheck(string filename, string[] allLines)
          {
              foreach (string nextLine in allLines.Skip(1))
              {
                  string[] tempLine;
                  if (isMRT)
                  {
                      tempLine = nextLine.Split(',');
                  }
                  else
                  {
                      tempLine = nextLine.Split('|');
                  }
                  logger.LogMessage("Checking Activity flag...");
                  if (tempLine[count].Length == 6)
                  {
                      checkColumn(tempLine);
                      logger.LogMessage("Activity flag is OK");
                      writeFile(tempLine, filename);
                  }
                  else if (tempLine[count].Length &gt; 6)
                  {
                      tempLine[count] = tempLine[count].Trim();
                      checkColumn(tempLine);
                      logger.LogMessage("FIXED Activity flag");
                      writeFile(tempLine, filename);                    
                  }
                  else
                  {
                      logger.LogMessage("The Activity flag did not have enough numbers in Row #: " +rowCount);
                      //throw new Exception("There are not enough numbers in the Activity flag");
                  }
  
  
    }
          }
  </code></pre>
  
  <p>edit2:
  The newly made temp file gets created in the Visual Studio folder in the bin folder of the Project. Not sure if that is useful for someone who has maybe run into this before.</p>