Solved

File not moving

Posted on 2012-03-16
7
332 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
  • 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now