title: C# - Program won't overwrite existing text file
body: <p>My system creates a text file on the first button click of the program and appends various things to it on other button clicks. Ideally everything would be saved all at the end but as it is a monitoring system that uses a timer, some of the data does need to be written to file at specific times or it will be lost.</p>
  
  <p>On the click of the first button (btnStart), the program locates/creates the main folder (given a name using a constant string) and locates/creates a sub folder (using text entered into a combination textboxes prior to the button click) this sets the directory for the text file as shown below in string (fileName):</p>
  
  <pre><code>    private void createDirectory()
      { //create output file in this folder using owner name and current date
  
          //main folder path (contains all files output from system)
          string rootDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Horse Monitoring Records";
          //sub folder path (each patient has individual subfolder)
          string subDirectory = rootDirectory + "\\" + txtPatName.Text + "/" + txtOwnerName.Text;
          //file name (patient has file created for each operation)
          fileName = subDirectory +  "\\" + txtOwnerName.Text + "/(" + DateTime.Now.Date.ToString("dd-MM-yyyy") + ").txt";
  
          if (!Directory.Exists(rootDirectory)) //if main folder does not exist...
          {
              Directory.CreateDirectory(rootDirectory); //create it in My Documents
          }
          if (!Directory.Exists(subDirectory)) //if patient sub folder does not exist...
          {
              Directory.CreateDirectory(subDirectory); //create it in Patient-Owner format
          }
      }
  </code></pre>
  
  <p>The next function called writes the first bit of text to the file, as shown:</p>
  
  <pre><code>    private void saveFileDetails()
      {
          //Once case details have been entered, create new file using these details and add data input structure
          StreamWriter consoleFile = new StreamWriter(fileName, true);
  
          consoleFile.WriteLine("------------------------------------------- Case Details -------------------------------------------");
          consoleFile.WriteLine("\n\n");
  
          //patient and vet details
          ...
          }
  </code></pre>
  
  <p>Originally when I set this function up, I did not include the 'true' overload to the StreamWriter in the first line of the function. This led to a run-time error that told me the text file could not be opened as it was already opened by another process.</p>
  
  <p>Since I added the 'true' overload, the system has worked fine, but I have just encountered a problem in that once a text file has been created, it cannot be overwritten as a text file normally would by a StreamWriter using the same path.</p>
  
  <p>I do need to be able to overwrite files as they become obselete after a period of time and do need to be replaced, does anyone have any idea where I can go from here to fix this issue?</p>
  
  <p>Thanks,
  Mark</p>