VB.Net 3.5 Check if file is in use

后端 未结 5 1644
甜味超标
甜味超标 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:02

    The same code using "using" :)

    Public Function FileInUse(ByVal sFile As String) As Boolean
     Dim thisFileInUse As Boolean = False
     If System.IO.File.Exists(sFile) Then
         Try
           Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                    ' thisFileInUse = False
           End Using
         Catch
           thisFileInUse = True
         End Try
     End If
     Return thisFileInUse
    End Function
    

提交回复
热议问题