Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

File not moving

Posted on 2012-03-16
7
Medium Priority
?
343 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 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