Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6224635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:42:21+00:00 2026-05-24T08:42:21+00:00

I have a excel file place on server. I am using a application which

  • 0

I have a excel file place on server.

I am using a application which written by VB.NET to open file in read only mode.

User 1 open file in read only mode.
How can user 2 detect that file is open or not status?

Thanks,
An

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T08:42:23+00:00Added an answer on May 24, 2026 at 8:42 am

    You need to test the file Read Only as ReadWrite will fail if the file is ReadOnly status.
    Below are some methods. Not sure where I got them all from except the last one by Randy Birch.
    Speed tests favour FileIsOpen3 and FileIsOpen4.

    Function FileIsOpen1(ByVal pathfile As String) As Boolean
        Dim ff As Integer
        If System.IO.File.Exists(pathfile) Then
            Try
                ff = FreeFile()
                Microsoft.VisualBasic.FileOpen(ff, pathfile, OpenMode.Binary, OpenAccess.Read, OpenShare.LockReadWrite)
                Return False
            Catch
                Return True
            Finally
                FileClose(ff)
            End Try
            Return True
        End If
    End Function
    
    Function FileIsOpen2(ByVal pathfile As String) As Boolean
        Dim stream As FileStream = Nothing
        Dim fi As FileInfo = Nothing
        If System.IO.File.Exists(pathfile) Then
            Try
                fi = New System.IO.FileInfo(pathfile)
                stream = fi.Open(FileMode.Open, FileAccess.Read, FileShare.None)
                Return True
            Catch generatedExceptionName As IOException
                Return False
            Finally
                If stream IsNot Nothing Then
                    stream.Close()
                End If
                fi = Nothing
            End Try
            Return True
        End If
    End Function
    
    Private Function FileIsOpen3(ByVal pathfile As String) As Boolean
        Try
            Dim fs As IO.FileStream = IO.File.Open(pathfile, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
            fs.Close()
            fs.Dispose()
            fs = Nothing
            Return False
        Catch ex As IO.IOException ' File open
            Return True
        Catch ex As Exception ' Unknown error
            Return True
        End Try
    End Function
    
    Private Declare Function CreateFile Lib "kernel32" _
       Alias "CreateFileA" _
      (ByVal lpFileName As String, _
       ByVal dwDesiredAccess As Long, _
       ByVal dwShareMode As Long, _
       ByVal lpSecurityAttributes As Long, _
       ByVal dwCreationDisposition As Long, _
       ByVal dwFlagsAndAttributes As Long, _
       ByVal hTemplateFile As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" _
      (ByVal hFile As Long) As Long
    
    ' Method
    Shared Function FileIsOpen4(ByVal pathfile As String) As Boolean
        ' Is File In Use ©1996-2009 Randy Birch
        ' http://vbnet.mvps.org/index.html?code/fileapi/createfile_inuse.htm
        Const GENERIC_READ As Long = &H80000000
        Const INVALID_HANDLE_VALUE As Long = -1
        Const OPEN_EXISTING As Long = 3
        Const FILE_ATTRIBUTE_NORMAL As Long = &H80
        Dim hFile As Long
    
        If System.IO.File.Exists(pathfile) Then
            Try
                ' note that FILE_ATTRIBUTE_NORMAL (&H80) has a different value than VB's constant vbNormal (0)!
                hFile = CreateFile(pathfile, GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
                ' this will evaluate to either -1 (File in use) or 0 (File free)
                Return hFile = INVALID_HANDLE_VALUE
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            Finally
                CloseHandle(hFile)
            End Try
        Else
            Return True
        End If
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a function which reads in an excel file and manipulates it.
I have tried to read an excel file using POI and then I wanted
I have an excel file which has more than 65536 rows. However, I can
I have an Excel file with one column which is filled with numbers. I
I'm using FormsAuthentication to allow users to login. However I have a Excel file
We have a file share on a debian server which people will be putting
I have a excel file which contains 5 sheets in it. The 1st sheet
I have a server (Server 2008) with IIS 7.5, an ASP.NET application. This application
I have an excel file which contains 250 sheets( 1st sheet contains 250 rows
I am building an application in Excel which will enable a user to select

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.