Results 1 to 5 of 5

Thread: how do you delete an open file through vb?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    does anyone know?
    NXSupport - Your one-stop source for computer help

  2. #2
    Guest
    I did see this program that deleted programs that were actually running.
    Don't know how it did it, but here's something that'll work.

    If you know the exe, here's how to close it:

    Code:
    Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
    Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
    Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    
    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 * MAX_PATH
        End Type
    
    
    Public Function KillApp(myName As String) As Boolean
        Const PROCESS_ALL_ACCESS = 0
        Dim uProcess As PROCESSENTRY32
        Dim rProcessFound As Long
        Dim hSnapshot As Long
        Dim szExename As String
        Dim exitCode As Long
        Dim myProcess As Long
        Dim AppKill As Boolean
        Dim appCount As Integer
        Dim i As Integer
        On Local Error GoTo Finish
        appCount = 0
        
        Const TH32CS_SNAPPROCESS As Long = 2&
        
        uProcess.dwSize = Len(uProcess)
        hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        rProcessFound = ProcessFirst(hSnapshot, uProcess)
        
        Do While rProcessFound
            i = InStr(1, uProcess.szexeFile, Chr(0))
            szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
            If Right$(szExename, Len(myName)) = LCase$(myName) Then
                KillApp = True
                appCount = appCount + 1
                myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
                AppKill = TerminateProcess(myProcess, exitCode)
                Call CloseHandle(myProcess)
            End If
            rProcessFound = ProcessNext(hSnapshot, uProcess)
        Loop
    
        Call CloseHandle(hSnapshot)
    Finish:
    End Function
    
    Usage:
    
    Call KillApp("C:\program.exe")
    And then you can delete it:

    Code:
    Kill ("C:\program.exe")

  3. #3
    Hyperactive Member
    Join Date
    Jan 1999
    Location
    Rotterdam, Netherlands
    Posts
    386
    Depending on what file it is... txt opened with notepad can be deleted.. when it's opened with word you can't..

    Uhmmm can you shorten you signature a little? It's almost one full screen (17" 1025x768) which is a bit way tooooooooooo big....
    Hope this helps

    Crazy D

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    I try to post my signature only once per thread

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    o yea, thanks bill, I mean Matt Gates (just pulling your leg)
    NXSupport - Your one-stop source for computer help

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width