none
Check if network .pdf is opened by other user process. RRS feed

  • Question

  • Hi,

    How can I check if a .pdf file that is placed on the shared network is opened by a user? I tried some google results but I did not find a good result.

        Public ReadOnly Property IsFileOpen As Boolean
            Get
                Dim stream As FileStream = Nothing
                Try
                    stream = File.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
                    stream.Close()
                    Return False
                Catch ex As Exception
    
                    If TypeOf ex Is IOException AndAlso IsFileLocked(ex) Then
                        ' do something here, either close the file if you have a handle, show a msgbox, retry  or as a last resort terminate the process - which could cause corruption and lose data
                        Return True
                    End If
                End Try
            End Get
        End Property

    Wednesday, January 16, 2019 2:38 PM

Answers

  • Hi,

    try the code:

    Public ReadOnly Property IsFileOpen(fileName As String) As Boolean
        Get
            Dim success As Boolean
            Dim stream As FileStream = Nothing
            Try
                stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None)
            Catch unusedIoException1 As Exception
             If TypeOf unusedIoException1 Is IOException AndAlso IsFileLocked(unusedIoException1) Then
             success = True
             End If
                
            Finally
                If stream IsNot Nothing Then
                    stream.Close()
                End If
            End Try
    
            Return success
    
        End Get
    End Property
    
    Private Shared Function IsFileLocked(exception As Exception) As Boolean
        Dim errorCode As Integer = Marshal.GetHRForException(exception) And ((1 << 16) - 1)
        Return errorCode = 32 OrElse errorCode = 33
    End Function

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by E_Jef Friday, January 18, 2019 12:24 PM
    Friday, January 18, 2019 7:40 AM

All replies

  • Hello,

    There is no 100 percent method to determine if a file is open. Some file types will obtain a lock while others will not. Also it's possible that a method learns the file is not open then when you proceed the file is locked in a few milliseconds later.

    There are several deep and length discussions on this topic over on Stackoverflow where there is no cut and dry final solution for learning if any file including a PDF is currently inuse.

    See this post which has an accepted answer close to what you have posted but is not 100 percent, as with yours

    https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 16, 2019 2:58 PM
    Moderator
  • Have you tried APIs like NetFileEnum ?
    Wednesday, January 16, 2019 3:03 PM
  • Hello,

    There is no 100 percent method to determine if a file is open. Some file types will obtain a lock while others will not. Also it's possible that a method learns the file is not open then when you proceed the file is locked in a few milliseconds later.

    There are several deep and length discussions on this topic over on Stackoverflow where there is no cut and dry final solution for learning if any file including a PDF is currently inuse.

    See this post which has an accepted answer close to what you have posted but is not 100 percent, as with yours

    https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

        Public ReadOnly Property IsFileOpen As Boolean
            Get
    
                Dim stream As FileStream = Nothing
                Try
                    stream = File.Open(FileMode.Open, FileAccess.Read, FileShare.None)
                Catch __unusedIOException1__ As IOException
                    Return True
                Finally
                    If stream IsNot Nothing Then stream.Close()
                End Try
    
                Return False
    
            End Get
        End Property
    System.ArgumentOutOfRangeException: 'Enum value was out of legal range.'

    This returns a immediate error. And I don't know what it means. Same error produced by my code block.

    Wednesday, January 16, 2019 3:19 PM
  • Hello,

    There is no 100 percent method to determine if a file is open. Some file types will obtain a lock while others will not. Also it's possible that a method learns the file is not open then when you proceed the file is locked in a few milliseconds later.

    There are several deep and length discussions on this topic over on Stackoverflow where there is no cut and dry final solution for learning if any file including a PDF is currently inuse.

    See this post which has an accepted answer close to what you have posted but is not 100 percent, as with yours

    https://stackoverflow.com/questions/876473/is-there-a-way-to-check-if-a-file-is-in-use


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

        Public ReadOnly Property IsFileOpen As Boolean
            Get
    
                Dim stream As FileStream = Nothing
                Try
                    stream = File.Open(FileMode.Open, FileAccess.Read, FileShare.None)
                Catch __unusedIOException1__ As IOException
                    Return True
                Finally
                    If stream IsNot Nothing Then stream.Close()
                End Try
    
                Return False
    
            End Get
        End Property
    System.ArgumentOutOfRangeException: 'Enum value was out of legal range.'

    This returns a immediate error. And I don't know what it means. Same error produced by my code block.

    Public ReadOnly Property IsFileOpen(fileName As String) As Boolean
        Get
            Dim success As Boolean
            Dim stream As FileStream = Nothing
            Try
                stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None)
            Catch unusedIoException1 As IOException
                success = True
            Finally
                If stream IsNot Nothing Then
                    stream.Close()
                End If
            End Try
    
            Return success
    
        End Get
    End Property



    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 16, 2019 5:18 PM
    Moderator
  • This line produces the error shown. And the exception is not a IOexception.

      stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None)

    Thursday, January 17, 2019 8:43 AM
  • Hi,

    try the code:

    Public ReadOnly Property IsFileOpen(fileName As String) As Boolean
        Get
            Dim success As Boolean
            Dim stream As FileStream = Nothing
            Try
                stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None)
            Catch unusedIoException1 As Exception
             If TypeOf unusedIoException1 Is IOException AndAlso IsFileLocked(unusedIoException1) Then
             success = True
             End If
                
            Finally
                If stream IsNot Nothing Then
                    stream.Close()
                End If
            End Try
    
            Return success
    
        End Get
    End Property
    
    Private Shared Function IsFileLocked(exception As Exception) As Boolean
        Dim errorCode As Integer = Marshal.GetHRForException(exception) And ((1 << 16) - 1)
        Return errorCode = 32 OrElse errorCode = 33
    End Function

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by E_Jef Friday, January 18, 2019 12:24 PM
    Friday, January 18, 2019 7:40 AM