开发者

Powershell - Check if file is in use

Is there a Powers开发者_如何学运维hell command to check if a file is in use by another user? If not, what would it take to write a script that can?


You can never tell if a file is currently being used only that it was recently being used. The reason why is that the moment the script returns the file could be closed by whatever program was previously using it. Writing scripts like this will only lead to flaky behavior.

A much better approach is to just do whatever the script was going to do if the file wasn't in use and catch the exceptions that result from a use conflict. The end result will be a much simpler and more reliable program.


There isn't a built-in command that I'm aware of however there are several tools you can use for this:

net file

From SysInternals on Technet (psfile and handle):

psfile.exe # lists and allows you to close remotely opened files

handle.exe | select-string ': File'


I find that using the SysInternals "ListDlls.exe" is pretty easy and convenient:

c:\SysInternals\Listdlls.exe -d AssemblyInUse.dll

ListDLLs v3.1 - List loaded DLLs
Copyright (C) 1997-2011 Mark Russinovich
Sysinternals - www.sysinternals.com

----------------------------------------------------------------------------
powershell.exe pid: 7296
Command line: "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe"

I hope that helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜