Solved

File not moving

Posted on 2012-03-16
7
340 Views
Last Modified: 2012-03-24
I'm using

Dim proc As New Diagnostics.Process()

//some code with a file

proc.WaitForExit(1000)
proc.Close()

File.Move()

I get an error saying the file is still in use.  How else can I close the process so I can move the file?
0
Comment
Question by:BTMExpert
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 14

Assisted Solution

by:systan
systan earned 250 total points
ID: 37731224
if your using the file as stream, trace the codes where it ends, and close the stream, eq., stream1.close

or else, you might kill the file with proc.kill before moving it
0
 

Author Comment

by:BTMExpert
ID: 37731265
I've tried .WaitForExit, Close, Kill, and Dispose.  None are working.  I still get that error.  I put  a timestamp after every instance of proc to see how long it took to finish and it's pretty instant.  I have 3 instances running.  So my code is like this
Dim proc As New Diagnostics.Process()

For loop

proc.Start()
proc.close()

proc.Start()
proc.close()

proc.Start()
proc.close()

File.Move

Next

Open in new window

I've also tried
Dim proc As New Diagnostics.Process()

For loop

proc.Start()


proc.Start()


proc.Start()
proc.close()

File.Move

Next

Open in new window

to no success
0
 
LVL 14

Expert Comment

by:systan
ID: 37732081
It's impossible that you can't kill the process unless it has been hold by anything else in the service.

Try to find out the main controller of the application you want to kill

or you have not pointed out the right process to kill
eq.
For Each p As Process In Process.GetProcesses
            If p.ProcessName = "WINWORD" Then
                p.Kill()
            End If
            If p.ProcessName = "EXCEL" Then
                p.Kill()
            End If
            If p.ProcessName = "MSPUB" Then
                p.Kill()
            End If
        Next

Open in new window


or you could try this code to:
      Private Delegate Function EnumWindowsProc(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
    <DllImport("USER32.DLL")> _
           Private Shared Function GetShellWindow() As IntPtr
    End Function
    <DllImport("USER32.DLL")> _
    Private Shared Function GetWindowTextLength(ByVal hWnd As IntPtr) As Integer
    End Function
    <DllImport("USER32.DLL")> _
    Private Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As Boolean
    End Function
    <DllImport("USER32.DLL")> _
    Private Shared Function EnumWindows(ByVal enumFunc As EnumWindowsProc, ByVal lParam As Integer) As Boolean
    End Function
    Private hShellWindow As IntPtr = GetShellWindow()
    Private dictWindows As New Dictionary(Of IntPtr, String)
    Private currentProcessID As Integer
    <System.Runtime.InteropServices.DllImport("kernel32.dll")> _
    Private Shared Function TerminateThread(ByVal hThread As IntPtr, ByVal dwExitCode As UInteger) As Boolean
    End Function
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function PostMessage(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wparam As IntPtr, ByVal lparam As IntPtr) As Boolean
    End Function
    
    Public Function GetOpenWindowsFromPID(ByVal processID As Integer) As IDictionary(Of IntPtr, String)
        dictWindows.Clear()
        currentProcessID = processID
        EnumWindows(AddressOf enumWindowsInternal, 0)
        Return dictWindows
    End Function


  Private Function enumWindowsInternal(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
        If (hWnd <> hShellWindow) Then
            Dim windowPid As UInt32
            If Not IsWindowVisible(hWnd) Then
                Return True
            End If
            Dim length As Integer = GetWindowTextLength(hWnd)
            If (length = 0) Then
                Return True
            End If
            GetWindowThreadProcessId(hWnd, windowPid)
            If (windowPid <> currentProcessID) Then
                Return True
            End If
            Dim stringBuilder As New StringBuilder(length)
            GetWindowText(hWnd, stringBuilder, (length + 1))
            dictWindows.Add(hWnd, stringBuilder.ToString)
        End If
        Return True
    End Function

    Private Function searchndestroy(ByVal dfilename As String) As Boolean
        Dim found As Boolean = False
        Dim ext As String = Nothing
        For Each proces As Process In Process.GetProcesses
            Dim windows As IDictionary(Of IntPtr, String) = GetOpenWindowsFromPID(proces.Id)
            For Each kvp As KeyValuePair(Of IntPtr, String) In windows
                If kvp.ToString.Contains(dfilename) Then
                    proces.Kill()
                    found = True
                    Exit For
                End If
		''//or
		If kvp.Value = ("Microsoft Office Activation Wizard") Then
		    PostMessage(kvp.Key, &H10, 0, 0)
                    ''//TerminateThread(kvp.key,1)
		    found = True
                    Exit For
                End If
            Next
            If found = True Then Exit For
        Next
''//you can use LISTBOX to view the kvp.value of the application
   End Function

Open in new window


if still not killing?  it means somethings holding the process, an application in the services.
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 14

Expert Comment

by:systan
ID: 37732082
It's impossible that you can't kill the process unless it has been hold by anything else in the service.

Try to find out the main controller of the application you want to kill

or you have not pointed out the right process to kill
eq.
For Each p As Process In Process.GetProcesses
            If p.ProcessName = "WINWORD" Then
                p.Kill()
            End If
            If p.ProcessName = "EXCEL" Then
                p.Kill()
            End If
            If p.ProcessName = "MSPUB" Then
                p.Kill()
            End If
        Next

Open in new window


or you could try this code to:
      Private Delegate Function EnumWindowsProc(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
    <DllImport("USER32.DLL")> _
           Private Shared Function GetShellWindow() As IntPtr
    End Function
    <DllImport("USER32.DLL")> _
    Private Shared Function GetWindowTextLength(ByVal hWnd As IntPtr) As Integer
    End Function
    <DllImport("USER32.DLL")> _
    Private Shared Function IsWindowVisible(ByVal hWnd As IntPtr) As Boolean
    End Function
    <DllImport("USER32.DLL")> _
    Private Shared Function EnumWindows(ByVal enumFunc As EnumWindowsProc, ByVal lParam As Integer) As Boolean
    End Function
    Private hShellWindow As IntPtr = GetShellWindow()
    Private dictWindows As New Dictionary(Of IntPtr, String)
    Private currentProcessID As Integer
    <System.Runtime.InteropServices.DllImport("kernel32.dll")> _
    Private Shared Function TerminateThread(ByVal hThread As IntPtr, ByVal dwExitCode As UInteger) As Boolean
    End Function
    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Private Shared Function PostMessage(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wparam As IntPtr, ByVal lparam As IntPtr) As Boolean
    End Function
    
    Public Function GetOpenWindowsFromPID(ByVal processID As Integer) As IDictionary(Of IntPtr, String)
        dictWindows.Clear()
        currentProcessID = processID
        EnumWindows(AddressOf enumWindowsInternal, 0)
        Return dictWindows
    End Function


  Private Function enumWindowsInternal(ByVal hWnd As IntPtr, ByVal lParam As Integer) As Boolean
        If (hWnd <> hShellWindow) Then
            Dim windowPid As UInt32
            If Not IsWindowVisible(hWnd) Then
                Return True
            End If
            Dim length As Integer = GetWindowTextLength(hWnd)
            If (length = 0) Then
                Return True
            End If
            GetWindowThreadProcessId(hWnd, windowPid)
            If (windowPid <> currentProcessID) Then
                Return True
            End If
            Dim stringBuilder As New StringBuilder(length)
            GetWindowText(hWnd, stringBuilder, (length + 1))
            dictWindows.Add(hWnd, stringBuilder.ToString)
        End If
        Return True
    End Function

    Private Function searchndestroy(ByVal dfilename As String) As Boolean
        Dim found As Boolean = False
        Dim ext As String = Nothing
        For Each proces As Process In Process.GetProcesses
            Dim windows As IDictionary(Of IntPtr, String) = GetOpenWindowsFromPID(proces.Id)
            For Each kvp As KeyValuePair(Of IntPtr, String) In windows
                If kvp.ToString.Contains(dfilename) Then
                    proces.Kill()
                    found = True
                    Exit For
                End If
		''//or
		If kvp.Value = ("Microsoft Office Activation Wizard") Then
		    PostMessage(kvp.Key, &H10, 0, 0)
                    ''//TerminateThread(kvp.key,1)
		    found = True
                    Exit For
                End If
            Next
            If found = True Then Exit For
        Next
''//you can use LISTBOX to view the kvp.value of the application
   End Function

Open in new window


if still not killing?  it means somethings holding the process, an application in the services.
0
 

Author Comment

by:BTMExpert
ID: 37737221
When I try to run the Kill(), it gives me an error "Access is denied"

I'm using ffmpeg to convert video files if that makes a difference.  It didn't think it would that's why I don't know why p.Kill() doesn't work.  Thank you for your help so far.
0
 

Accepted Solution

by:
BTMExpert earned 0 total points
ID: 37737357
proc.Exited is what i needed.
0
 

Author Closing Comment

by:BTMExpert
ID: 37760253
In this case, this was what I was looking for.  I didn't know there was a Exited().  Thank you systan
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question