On a Windows Server 2012 R2 system, a Kotlin program uses FileChannel.tryLock()
to hold an exclusive lock on a file, like this:
val fileRw = RandomAccessFile(file, "rw")
fileRw.channel.tryLock()
With this lock in place, I cannot open the file with:
- WordPad
- Notepad++
Programmatically with C#, for any value of
FileShare
:using (var fileStream = new FileStream(processIdPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var textReader = new StreamReader(fileStream)) { textReader.ReadToEnd(); }
From the command line, the
type
command:C:\some-directory>type file.txt The process cannot access the file because another process has locked a portion of the file.
Internet Explorer (yes, I was desperate)
I can open it with Notepad.
How the heck is Notepad able to open a locked file that nothing else can?