title: Notepad beats them all?
body: <p>On a Windows Server 2012 R2 system, a Kotlin program uses <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/channels/FileChannel.html#tryLock()" rel="noreferrer"><code>FileChannel.tryLock()</code></a> to hold an exclusive lock on a file, like this:</p>
  
  <pre class="lang-kotlin prettyprint-override"><code>val fileRw = RandomAccessFile(file, "rw")
  fileRw.channel.tryLock()
  </code></pre>
  
  <p>With this lock in place, I <em>cannot</em> open the file with:</p>
  
  <ul>
  <li>WordPad</li>
  <li>Notepad++</li>
  <li><p>Programmatically with C#, for any value of <code>FileShare</code>:</p>
  
  <pre class="lang-c# prettyprint-override"><code>using (var fileStream = new FileStream(processIdPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  using (var textReader = new StreamReader(fileStream))
  {
      textReader.ReadToEnd();
  }
  </code></pre></li>
  <li><p>From the command line, the <a href="https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/type" rel="noreferrer"><code>type</code></a> command:</p>
  
  <pre class="lang-none prettyprint-override"><code>C:\some-directory&gt;type file.txt
  The process cannot access the file because another process has locked a portion of the file.
  </code></pre></li>
  <li><p>Internet Explorer (yes, I was desperate)</p></li>
  </ul>
  
  <p>I <em>can</em> open it with Notepad.</p>
  
  <p>How the heck is Notepad able to open a locked file that nothing else can?</p>