最佳答案
在 WindowsServer2012R2系统上,Kotlin 程序使用 FileChannel.tryLock()
来保持文件的独占锁,如下所示:
val fileRw = RandomAccessFile(file, "rw")
fileRw.channel.tryLock()
锁定后,我用以下命令打开文件:
对于任何值的 FileShare
,使用 C # 编程:
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?