请问如何获得一个文件的状态,如是否被其它程序运行?因为我在程序中要删除一个文件,但当这个文件在运行的时候就不能删除了,所以我想在事先的时候就获得这个文件的状态,请问如何获得?

解决方案 »

  1.   

    ''try:
    private sub ...
        if FileInUse("file name") then 
            msgbox "File is in use!"
        else
            ...
        end if
    end subPublic Function FileInUse(path$) As Boolean
        Dim X
        X = FreeFile ''''get free file #    On Error Resume Next
        Open path$ For Input As X
        If err = 0 Then
            FileExists = True
        Else
            FileExists = False
        End If
        
        Close XEnd Function
      

  2.   

    sorry~~~~ errors...=============================
    ''try :private sub ...
        if FileInUse("file name") then 
            msgbox "File is in use!"
        else
            ...
        end if
    end subPublic Function FileInUse(path$) As Boolean
        Dim X
        X = FreeFile ''''get free file #    On Error Resume Next
        Open path$ For Input As X
        If err = 0 Then
            FileInUse= True
        Else
            FileInUse= False
        End If
        
        Close XEnd Function
      

  3.   

    参考
    http://www.china-askpro.com/msg6/qa35.shtml