记事本打败了他们?

某些process.id文件被Windows Server 2012 R2上的某个进程锁定。
无法打开它:

  • 写字板,
  • 记事本+ +,
  • 以编程方式在C#中使用FileSharing的各种值,

    using (var fileStream = new FileStream( processIdPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var textReader = new StreamReader(fileStream)) { String processIdString = textReader.ReadToEnd(); node.processId = Convert.ToInt32(processIdString); } 
  • 从命令行输入“type”:

     C:\some-directory> type process.id The process cannot access the file because another process has locked a portion of the file. 

  • IE(是的,我很绝望)

可以用记事本打开它

记事本能够打开一个没有其他东西可以锁定的文件?

编辑
文件锁定代码是使用来自FileChannel的tryLock的 kotlin / java

  val pidFileRw = RandomAccessFile(pidFile, "rw") val pidFileLock = pidFileRw.channel.tryLock() 

基本上它是要求独家锁定。 但记事本仍然可以得到它。

记事本首先将文件映射到内存中,而不是使用其他编辑器所使用的“常用”文件读取机制。 该方法允许读取文件,即使它们具有独占的基于范围的锁定。

您可以在C#中实现相同的功能,其中包括:

 using (var f = new FileStream(processIdPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using (var m = MemoryMappedFile.CreateFromFile(f, null, 0, MemoryMappedFileAccess.Read, null, HandleInheritability.None, true)) { using (var s = m.CreateViewStream(0, 0, MemoryMappedFileAccess.Read)) { using (var r = new StreamReader(s)) { var l = r.ReadToEnd(); Console.WriteLine(l); } } } } 
Interesting Posts