.net 如何检查文件是否正在被另一个进程使用 - Powershell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9394629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if file is being used by another process - Powershell
提问by user983965
I am trying to find a solution which will check whether a file is being used by another process. I don't want to read the contents of the file, as on a 7GB document, this could take a while. Currently I am using the function mentioned below, which is not ideal as the script takes about 5 - 10 minutes to retrieve a value.
我正在尝试找到一种解决方案来检查文件是否正在被另一个进程使用。我不想阅读文件的内容,因为在 7GB 的文档中,这可能需要一段时间。目前我正在使用下面提到的函数,这并不理想,因为脚本需要大约 5 - 10 分钟来检索一个值。
function checkFileStatus($filePath)
{
write-host (getDateTime) "[ACTION][FILECHECK] Checking if" $filePath "is locked"
if(Get-Content $filePath | select -First 1)
{
write-host (getDateTime) "[ACTION][FILEAVAILABLE]" $filePath
return $true
}
else
{
write-host (getDateTime) "[ACTION][FILELOCKED] $filePath is locked"
return $false
}
}
Any help would be greatly appreciated
任何帮助将不胜感激
回答by user983965
Created a function which solves the above problem:
创建了一个解决上述问题的函数:
function checkFileStatus($filePath)
{
write-host (getDateTime) "[ACTION][FILECHECK] Checking if" $filePath "is locked"
$fileInfo = New-Object System.IO.FileInfo $filePath
try
{
$fileStream = $fileInfo.Open( [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read )
write-host (getDateTime) "[ACTION][FILEAVAILABLE]" $filePath
return $true
}
catch
{
write-host (getDateTime) "[ACTION][FILELOCKED] $filePath is locked"
return $false
}
}
回答by Florent Breheret
The function i use to check whether a file is locked or not :
我用来检查文件是否被锁定的函数:
function IsFileLocked([string]$filePath){
Rename-Item $filePath $filePath -ErrorVariable errs -ErrorAction SilentlyContinue
return ($errs.Count -ne 0)
}
回答by Piper
function IsFileAccessible( [String] $FullFileName )
{
[Boolean] $IsAccessible = $false
try
{
Rename-Item $FullFileName $FullFileName -ErrorVariable LockError -ErrorAction Stop
$IsAccessible = $true
}
catch
{
$IsAccessible = $false
}
return $IsAccessible
}
回答by CB.
Check this script on poschcode.org:
在poschcode.org上检查此脚本:
filter Test-FileLock {
if ($args[0]) {$filepath = gi $(Resolve-Path $args[0]) -Force} else {$filepath = gi $_.fullname -Force}
if ($filepath.psiscontainer) {return}
$locked = $false
trap {
Set-Variable -name locked -value $true -scope 1
continue
}
$inputStream = New-Object system.IO.StreamReader $filepath
if ($inputStream) {$inputStream.Close()}
@{$filepath = $locked}
}
回答by Trevor Sullivan
SInce you don't want to read the file, I would recommend using a utility like Sysinternals Handle.exe, which will spit out all open handles for a process. You can download Handle.exe from here:
由于您不想读取该文件,我建议您使用 Sysinternals Handle.exe 之类的实用程序,它会吐出进程的所有打开句柄。您可以从这里下载 Handle.exe:
http://technet.microsoft.com/en-us/sysinternals/bb896655
http://technet.microsoft.com/en-us/sysinternals/bb896655
You can run Handle.exe without any arguments, and it will return all open file handles. You can parse the output if necessary, or just match the output against your full file path.
您可以不带任何参数运行 Handle.exe,它将返回所有打开的文件句柄。如有必要,您可以解析输出,或者仅将输出与完整文件路径相匹配。

