Check the username who locks/uses a specific file

Hi folks,

Is there any solution to extract the name of the user who locks/has a specific file open?
With a basic try-catch, I can check if the file is editable or not, but I would need to notify the client who locks the file so they can act immediately.

Thank you!

Please refer below vb.net code, you can invoke it in Uipath.

Private Sub Workbook_Open()

Dim Folder As String
Dim FName As String
Dim strFilename As String
Folder = “C:”
FName = “data.xls”
If Not FileLocked(Folder & FName) Then
MsgBox “File " & Folder & FName & " is not open”
Else
MsgBox "The file is locked by " & GetFileOwner(Folder, FName) & “.”
End If
End Sub

Function GetFileOwner(fileDir As String, fileName As String) As String

'On Error Resume Next
Dim secUtil As Object
Dim secDesc As Object
Set secUtil = CreateObject(“ADsSecurityUtility”)
Set secDesc = secUtil.GetSecurityDescriptor(fileDir & fileName, 1, 1)
GetFileOwner = secDesc.Owner
End Function

Function FileLocked(strFilename As String) As Boolean
On Error Resume Next
’ If the file is already opened by another process,
’ and the specified type of access is not allowed,
’ the Open operation fails and an error occurs.
Open strFilename For Binary Access Read Write Lock Read Write As #1
Close #1
’ If an error occurs, the document is currently open.
If Err.Number <> 0 Then
’ Display the error number and description.
'MsgBox “Error #” & Str(Err.Number) & " - " & Err.Description
FileLocked = True
Err.Clear
End If
End Function

Can you please let me know if it Is .Net code or Vbs code?
Because while using it in Invoke Code, I am getting errors.

Based on syntax, this appears to be VBA code, not VB.NET

EDIT: quick google search shows it was taken directly from this page: SOLIDWORKS Forums

Thanks for the confirmation @Dave

I am having a file which is in the shared folder and can be accessible by the multiple users.
When the bot is executing and it wants to access that file it will through an exception stating “The file is being used by another user.”’

So, here in the catch block, I want to know the name of the person who is using that file. Can you help me on this?

Thank in Advance!

If you are able to run a macro on that workbook/sheet, you can use the ‘invoke macro’ to run the macro. This requires you to store the macro as a .vbs file (with slight alterations), or to store the macro in the referenced workbook (requiring extension to be .xlsm or .xlsb), or stored in the personal workbook (and thus requiring each robot running the code to have the same macro in their personal workbook).

ALternatively, you can try out the solution presented here: An app to see who has files open on a network server - CodeProject

NOTE: I have not tried either method myself, so I can’t guarantee either code works

Yes! VBA can be used for workbooks right? What if my file is not Excel?
I tried this

System.IO.File.GetAccessControl(path).GetOwner(GetType(System.Security.Principal.NTAccount)).Value

But i am getting the Current user running the bot.

If it isn’t excel, then the VBA solution will not work. I’d try the vb.net code referenced in the link above. I haven’t ever done this myself so I can’t give much more help than that, sorry!

It looks like that is getting the “owner” property of the file, which is not going to be the current user locking the file

Yeah, Is it not like who edited the file for last time can’t be the Owner?
please, let me know if i am wrong.

Owner != who edited the file last.

If you want to see who edited the file last, that shows up as ‘Last saved by’ and can be accessed using the WindowsAPICodePack.Shell libraries. See this stackoverflow post: c# - Retrieve 'Last Saved By' file property programmatically - Stack Overflow

You can also see the original creator using the same WindowsAPICodePack.Shell library and check the System.Author property (instead of the .LastAuthor property). See documentation here: System (Windows) | Microsoft Docs