我认为PowerShell可能是完成此任务的最简洁的方法。类似于以下内容:
#Specify the file name
$fileName = "C:\myfile.txt"
#Open the file in read only mode, without sharing (I.e., locked as requested)
$file = [System.io.File]::Open($fileName, 'Open', 'Read', 'None')
#Wait in the above (file locked) state until the user presses a key
Write-Host "Press any key to continue ..."
$null = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
#Close the file (This releases the current handle and unlocks the file)
$file.Close()
暂停时,以上脚本在尝试打开“ myfile.txt”时引起以下提示: