none
Accessing list of open sessions and open files - a better way to do it? RRS feed

  • Question

  • Good evening all

    I need to get a list of open files and open sessions in my app so I can run it my servers and get required info

    Obviously I can use OpenFiles.Exe and Net Session, run them using Process.Start, go through results and show in a listview or similar (which I have done)

    But just wondered if there was any API or .NET native method for getting this information that anyone knows of?

    Tools such as SysInternals PSFile do it and it says it uses NET API?

    If it is not possible then I will stick with OpenFiles/Net Session but just thought I would ask the experts :)


    Darren Rose

    Tuesday, June 27, 2017 8:16 PM

All replies

  • P.S. Seen mentions of NetSessionEnum and NetFileEnum API's - but all code and articles seem to be years old and for older version of Windows so not sure if still relevant or not

    Darren Rose

    Tuesday, June 27, 2017 8:23 PM
  • It is a bit complicated in VB.NET because for example OpenFiles calls NtQuerySystemInformation()

    I tested it with SystemHandleInformation (OpenFiles uses SystemExtendedHandleInformation), it works, but it needs a lot of declarations for structures (easier in C++)

    Tuesday, June 27, 2017 8:48 PM
  • Okay thanks for your reply

    Shame .NET doesn't have some built in support


    Darren Rose

    Wednesday, June 28, 2017 12:49 PM
  • Okay thanks for your reply

    Shame .NET doesn't have some built in support


    Darren Rose


    Have you investigated WMI objects to see if you can get the information that way?  Seems likely based on the old Win32 API stuff referenced above...

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, June 28, 2017 2:50 PM
    Moderator
  • Hi Reed

    Thanks for you reply

    Yes I had taken a look at WMI and from what I have found it seems quite limited - I can see open sessions but cannot see open files it would appear

    I looked at Win32_ConnectionShare, Win32_Share and Win32_ServerConnection


    Darren Rose

    Wednesday, June 28, 2017 3:24 PM
  • I did a search for "using WMI to enumerate open files" and found a couple of hits referring to using LANMAN in WMI.

    https://blogs.technet.microsoft.com/heyscriptingguy/2005/02/16/how-can-i-list-open-sessions-and-open-files-on-a-computer/

    The results tend to be around PowerShell and other scripting, but the WMI interactions would be similar in VB.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, June 28, 2017 3:52 PM
    Moderator
  • Yes I found that one too, but seemed to be all wscript and can't see how it would work in WMI as looking at the Win32_Service and Lanman Server just shows information on that service with methods to start, stop and all the usual rather than anything to show open files

    Will keep looking and trying things

    Thanks


    Darren Rose

    Wednesday, June 28, 2017 4:02 PM
  • Yes I found that one too, but seemed to be all wscript and can't see how it would work in WMI as looking at the Win32_Service and Lanman Server just shows information on that service with methods to start, stop and all the usual rather than anything to show open files

    Will keep looking and trying things

    Thanks


    Darren Rose

    I'm not sure what you mean by "just shows information on that service"... you have to query WMI for the specific info you want and then create accessors for the particular properties of the queried object.  In this case it looks like you want the "Sessions" and "Resources" properties of the LanmanServer.

    As noted the reference material does tend to target one scripting interface or another but you should still be able to deduce the WMI object hierarchy being used by the script and implement the same thing in VB with the WMI managed objects.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, June 28, 2017 4:08 PM
    Moderator
  • Yes have now found a couple of examples of doing it - so will give them a try

    Thanks


    Darren Rose

    Wednesday, June 28, 2017 4:09 PM
  • Okay seem to have a very rough solution working as below - code very rough at this stage but it does provide me with some results

    Just need to clean up / improve and work out bits like why session computer name returning IP address rather than the actual computer name.  And why I can't get number of open files like I can see in normal windows gui

    Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim Server As String = Environment.MachineName Dim objfs As Object = GetObject("WinNT://" & Server & "/lanmanserver,fileservice") TextBox1.AppendText("RESOURCES:" & vbCrLf) TextBox1.AppendText("==========" & vbCrLf) For Each objres As Object In objfs.Resources TextBox1.AppendText("Resource Name: " & objres.Name & vbCrLf) TextBox1.AppendText("User: " & objres.User & vbCrLf) TextBox1.AppendText("Path: " & objres.Path & vbCrLf) TextBox1.AppendText("Lock Count: " & objres.LockCount & vbCrLf) TextBox1.AppendText("" & vbCrLf) Next TextBox1.AppendText("") TextBox1.AppendText("SESSIONS:" & vbCrLf) TextBox1.AppendText("=========" & vbCrLf) For Each objses As Object In objfs.Sessions TextBox1.AppendText("Session Object Name: " & objses.Name & vbCrLf) TextBox1.AppendText("Client Computer Name: " & objses.Computer & vbCrLf) TextBox1.AppendText("Seconds connected: " & ConvertSecToHMS(objses.ConnectTime) & vbCrLf) TextBox1.AppendText("Seconds idle: " & ConvertSecToHMS(objses.IdleTime) & vbCrLf) TextBox1.AppendText("Connected User: " & objses.User & vbCrLf) TextBox1.AppendText("" & vbCrLf) Next End Sub

    Public Function ConvertSecToHMS(myValue As Long) As String
            Dim ts As TimeSpan = TimeSpan.FromSeconds(myValue)
            Return Int(ts.TotalHours).ToString("00") + ":" + Int(ts.Minutes).ToString("00") + ":" + ts.Seconds.ToString("00")
    End Function

    End Class



    Darren Rose


    • Edited by wingers Wednesday, June 28, 2017 7:41 PM
    Wednesday, June 28, 2017 7:18 PM
  • EDIT: code amended above to show seconds connected / idle as h:m:s rather than just seconds

    Darren Rose


    • Edited by wingers Wednesday, June 28, 2017 8:05 PM
    Wednesday, June 28, 2017 7:40 PM