title: How to lock a file with C#?
body: <p>I'm not sure what people usually mean by "lock" a file, but what I want is to do that thing to a file that will produce a "The specified file is in use" error message when I try to open it with another application.</p>
  
  <p>I want to do this to test my application to see how it behaves when I try to open a file that is on this state. I tried this:</p>
  
  <pre><code>FileStream fs = null;
  
  private void lockToolStripMenuItem_Click(object sender, EventArgs e)
  {
      fs = new FileStream(@"C:\Users\Juan Luis\Desktop\corte.txt", FileMode.Open);
  }
  
  private void unlockToolStripMenuItem_Click(object sender, EventArgs e)
  {
      fs.Close();
  }
  </code></pre>
  
  <p>But apparently it didn't do what I expected because I was able to open the file with Notepad while it was "locked". So how can I lock a file so it cannot be opened with another application for my testing purposes?</p>