If the file is in use following function will return false.
VB.NET
- 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
C#
- public bool FileInUse(string sFile)
- {
- bool thisFileInUse = false;
- if (System.IO.File.Exists(sFile)) {
- try {
- using (System.IO.FileStream f = new System.IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
- // thisFileInUse = False
- }
- } catch {
- thisFileInUse = true;
- }
- }
- return thisFileInUse;
- }