请问,如何判断一个文件已经在运行之中.有人想出办法,就是试着删除OR改名,不成功就在运行,否则就不在运行.是不是有更的方法,请大侠出手.

解决方案 »

  1.   

    可以这样判断文件是否已经打开:Function IsOpen(sFile As String) As Boolean
         Dim fFile As Integer
         fFile = FreeFile()
         On Error GoTo ErrOpen
         Open sFile For Binary Lock Read Write As fFile
         Close fFile
         Exit Function
    ErrOpen:
         If Err.Number <> 70 Then
            Msg = "Error # " & Str(Err.Number) & " was generated by " _
            & Err.Source & Chr(13) & Err.Description
            MsgBox Msg, , "Error", Err.HelpFile, Err.HelpContext
         Else
            IsOpen = True
         End If
    End Function
      

  2.   

    Public Sub subFindProcess(ByVal strProcess As String)
        Dim strComputer As String
        Dim objWMIService As Object
        Dim colProcessList
        Dim objProcess As ObjectOn Error Resume Nextt
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Set colProcessList = objWMIService.ExecQuery _
            ("Select * from Win32_Process Where Name = '" & strProcess & "'    
        For Each objProcess In colProcessList
            Public Sub subKillProcess(ByVal strProcess As String)
        Dim strComputer As String
        Dim objWMIService As Object
        Dim colProcessList
        Dim objProcess As ObjectOn Error Resume Next
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        Set colProcessList = objWMIService.ExecQuery _
            ("Select * from Win32_Process Where Name = '" & strProcess & "'    For Each objProcess In colProcessList
            Debug.Print colProcessList
        Next
        End Sub
    Nextt:
    End Sub
      

  3.   

    你是指的某文件是否正在被占用吧,如果是以独占方式打开的文件并且在打开过程中,那么在其它程序试图去访问的时候,就会提示当前文件正在使用.如果是判断某程序是否在执行的话,那么可以判断进程是否存在,如果在同一套程序里,判断是否当前程序已经启动,用app.preinstance判断就可以
      

  4.   

    在发这个贴子时,已找到了方法,下面的这种大家看看如何Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Private Declare Function Process32First Lib "kernel32" (ByVal hSnapshot As Long, lppe As PROCESSENTRY32) As Long
    Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As LongPrivate Type PROCESSENTRY32
        dwSize As Long
        cntUsage As Long
        th32ProcessID As Long
        th32DefaultHeapID As Long
        th32ModuleID As Long
        cntThreads As Long
        th32ParentProcessID As Long
        pcPriClassBase As Long
        dwFlags As Long
        szExeFile As String * 1024
    End Type
    Const TH32CS_SNAPPROCESS = &H2 Function IsOpen(hfile As String) As Boolean
      Dim my As PROCESSENTRY32
      Dim l As Long
      Dim l1 As Long
      
      l = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
      If l Then
        my.dwSize = 1060
        If (Process32First(l, my)) Then 
          Do
          If Left(Trim(my.szExeFile), 10) = Left(hfile, 10) Then IsOpen = True '针对具体文件,文件名是10 个字符就是10
          Loop Until (Process32Next(l, my) < 1) '遍历所有进程知道返回值为False
        If IsOpen Then GoTo aa
        End If
        IsOpen = False
    aa:
        
        l1 = CloseHandle(l)
      End If
    End Function