VB.Net 3.5 Check if file is in use

后端 未结 5 1693
甜味超标
甜味超标 2021-01-15 03:43

I have an update program that is completely independent of my main application. I run the update.exe to update the app.exe. To check to see if the file is in use, I move it

5条回答
  •  半阙折子戏
    2021-01-15 04:07

    This is what I ended up doing. The network scan did not work on a peer-to-peer network. It didn't resolve the other computers at all.

    Anyhow, here it is:

    Private Sub CheckFileIfFileIsInUse(ByVal thefilename As String)
    Try
    ' on the Vista OS you can rename a file that is in use
    ' the 'in use' of the original file follows the new file name

          Dim testfile As String = thefilename & ".tst"
    If IO.File.Exists(testfile) Then
    IO.File.Delete(testfile)
    End If
    ' so we have to rename it to something else
    IO.File.Move(thefilename, testfile)
    ' copy it back to the original file name in case something
    breaks ' this just keeps them in working order if it does
    IO.File.Copy(testfile, thefilename)
    ' then we try to delete the original file that could be in use
    ' which will return an 'in use' error at this point
    If IO.File.Exists(testfile) Then
    IO.File.Delete(testfile)
    End If
    Catch ex As Exception
    'throw it to the originating method
    Throw
    End Try

    End Sub

    Hope this helps the next person.

提交回复
热议问题