找回密码
 加入
搜索
查看: 1987|回复: 7

[系统综合] 判斷文件是否正在使用

  [复制链接]
发表于 2010-8-26 12:23:24 | 显示全部楼层 |阅读模式
如何判斷文件是否正在使用,期盼高手解答
发表于 2010-8-26 12:53:15 | 显示全部楼层
先复制一个。然后删除。看看成不成功。
成功就没在用。把备份的复制回来。
没成功就在用。把备份的删除。
发表于 2010-8-26 14:35:06 | 显示全部楼层
之前也想過這個問題,但尚沒有確切有效的辦法,樓上的如果處理小文件可能還行,但如果1G或者更大的呢?
发表于 2010-8-26 15:17:54 | 显示全部楼层
一个文件若正在被使用,在使用者的进程空间中必然有这个文件的句柄,NtQuerySystemInformation可以枚举系统中的所有句柄,NtQueryObject取得句柄的对象名称。
发表于 2010-8-26 15:43:10 | 显示全部楼层
一个文件若正在被使用,在使用者的进程空间中必然有这个文件的句柄,NtQuerySystemInformation可以枚举系统中的所有句柄,NtQueryObject取得句柄的对象名称。
我怎么在帮助里么发现netquerysysteminformation
 楼主| 发表于 2010-8-27 10:11:33 | 显示全部楼层
在國外的論壇上好像搜索到了,大家看看:
$file = "d:\1.txt"
$h = FileOpen($file, 1)
$x = _FileInUse($file)
MsgBox(0, "_FileInUse() example", "File "& $file & @CRLF & "Return= " & $x & "  (Error = " & @error & ")")
;FileClose($h)
;$x = _FileInUse($file)
;MsgBox(0, "_FileInUse() example", "File "& $file & @CRLF & "Return= " & $x & "  (Error = " & @error & ")")
;FileDelete($file)

;===============================================================================
;
; Function Name:    _FileInUse()
; Description:      Checks if file is in use
; Parameter(s):     $sFilename = File name
; Return Value(s):  1 - file in use (@error contains system error code)
;                   0 - file not in use
;
;===============================================================================
Func _FileInUse($sFilename)
    Local $aRet, $hFile
    $aRet = DllCall("Kernel32.dll", "hwnd", "CreateFile", _
                                    "str", $sFilename, _ ;lpFileName
                                    "dword", 0x80000000, _ ;dwDesiredAccess = GENERIC_READ
                                    "dword", 0, _ ;dwShareMode = DO NOT SHARE
                                    "dword", 0, _ ;lpSecurityAttributes = NULL
                                    "dword", 3, _ ;dwCreationDisposition = OPEN_EXISTING
                                    "dword", 128, _ ;dwFlagsAndAttributes = FILE_ATTRIBUTE_NORMAL
                                    "hwnd", 0) ;hTemplateFile = NULL
    $hFile = $aRet[0]
    If $hFile = -1 Then ;INVALID_HANDLE_VALUE = -1
        $aRet = DllCall("Kernel32.dll", "int", "GetLastError")
        SetError($aRet[0])
        Return 1
    Else
        ;close file handle
        DllCall("Kernel32.dll", "int", "CloseHandle", "hwnd", $hFile)
        Return 0
    EndIf
EndFunc

评分

参与人数 3威望 +1 金钱 +80 收起 理由
pusofalse + 30 学习了~
afan + 1 + 30
xsjtxy + 20

查看全部评分

发表于 2010-8-27 10:19:55 | 显示全部楼层
很强。收藏一下。会英文就是好啊。可以去国外文坛发掘。
发表于 2010-8-27 10:57:53 | 显示全部楼层
回复 6# xiaosen666m


    不错,这个还是很实用的东西啊。。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-9-22 01:11 , Processed in 0.088893 second(s), 24 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表