title: Overwrite an file which is currently open and it use in c#
body: <p>I am using C# to create a GUI which has a logging feature. When I create the log file, I provide it a filename. But I have the following issue. For instance, if the file is already open in the background on my desktop and I want to name my filename as the same, I get the exception message as following: </p>
  
  <pre><code>The process cannot access the file 'C:\blah\blah\mytest.csv' because it is being used by another process.
  </code></pre>
  
  <p>I am using the following piece of code to check if the file exists and using the streamwriter to create the new file.</p>
  
  <pre><code>if (FileUtils.Exists(fileName))
              {
                  if (!FileUtils.Delete(fileName))
                  {
                      cout&lt;&lt; Error replacing log file, it might be used by another process"; 
                  }             
              }
  
  //Exception handling
       public static bool Delete(string myfiletowrite)
              {
                  try
                  {
                      File.Delete(myfiletowrite);
                      return true;
                  }
                  catch (IOException)
                  {
                      return false;
                  }
              } 
  myfile= new StreamWriter(myfiletowrite, true); //exception occurs here when I try to write to myfiletowrite which is currently open 
  </code></pre>
  
  <p>I am unsure on how to close this file which is open in the background so I could write to this file. I would appreciate it someone could help me with this.</p>