Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!
I am a new user of the site, I would ask if you know a command (.net) to create a list of open files shared from a server.
I link the image of Microsoft tools.

thanks to all

http://imageshack.us/photo/my-images/11/fileaperti.jpg
Posted

1 solution

Take a look here

http://www.pinvoke.net/default.aspx/netapi32/NetFileEnum.html[^]


Also this could help (source CP)

C#
class Program
{
    static void Main(string[] args)
    {
        const int MAX_PREFERRED_LENGTH = -1;

        int dwReadEntries;
        int dwTotalEntries;
        IntPtr pBuffer = IntPtr.Zero;
        FILE_INFO_3 pCurrent = new FILE_INFO_3();

        int dwStatus = NetFileEnum(null, null, null, 3, ref pBuffer,
            MAX_PREFERRED_LENGTH, out dwReadEntries, out dwTotalEntries, IntPtr.Zero);

        if (dwStatus == 0)
        {
            for (int dwIndex = 0; dwIndex < dwReadEntries; dwIndex++)
            {

                IntPtr iPtr = new IntPtr(pBuffer.ToInt32() + (dwIndex * Marshal.SizeOf(pCurrent)));
                pCurrent = (FILE_INFO_3)Marshal.PtrToStructure(iPtr, typeof(FILE_INFO_3));

                Console.WriteLine("dwIndex={0}", dwIndex);
                Console.WriteLine("    id={0}", pCurrent.fi3_id);
                Console.WriteLine("    num_locks={0}", pCurrent.fi3_num_locks);
                Console.WriteLine("    pathname={0}", pCurrent.fi3_pathname);
                Console.WriteLine("    permission={0}", pCurrent.fi3_permission);
                Console.WriteLine("    username={0}", pCurrent.fi3_username);

            }

            NetApiBufferFree(pBuffer);
        }
    }

    [DllImport("netapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    static extern int NetFileEnum(
         string servername,
         string basepath,
         string username,
         int level,
         ref IntPtr bufptr,
         int prefmaxlen,
         out int entriesread,
         out int totalentries,
         IntPtr resume_handle
    );

    [DllImport("Netapi32.dll", SetLastError = true)]
    static extern int NetApiBufferFree(IntPtr Buffer);

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    struct FILE_INFO_3
    {
        public int fi3_id;
        public int fi3_permission;
        public int fi3_num_locks;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string fi3_pathname;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string fi3_username;
    }
}


Hope this helps

Cheers!
 
Share this answer
 
v2
Comments
Mirkox68 21-Sep-11 11:01am    
thanks but I do not understand how it works, the variable is always dwReadEntries = 0 and never comes in for

My code is in vb:

Imports System.Runtime.InteropServices

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Const MAX_PREFERRED_LENGTH As Integer = -1
Dim dwIndex, dwStatus, dwReadEntries, dwTotalEntries As Integer
Dim pCurrent As FILE_INFO_3
Dim iPtr, pBuffer As IntPtr

dwStatus = NetFileEnum(TextBox1.Text, Nothing, Nothing, 3, pBuffer, MAX_PREFERRED_LENGTH, dwReadEntries, dwTotalEntries, IntPtr.Zero)
If dwStatus = 0 Then
For dwIndex = 0 To dwReadEntries - 1

iPtr = New IntPtr(pBuffer.ToInt32 + (dwIndex * Marshal.SizeOf(pCurrent)))
pCurrent = CType(Marshal.PtrToStructure(iPtr, GetType(FILE_INFO_3)), FILE_INFO_3)

Debug.WriteLine("dwIndex=" & dwIndex)
Debug.WriteLine(" id: " & pCurrent.fi3_id)
Debug.WriteLine(" num_locks: " & pCurrent.fi3_num_locks)
Debug.WriteLine(" pathname: " & pCurrent.fi3_pathname)
Debug.WriteLine(" permission: " & pCurrent.fi3_permission)
Debug.WriteLine(" username: " & pCurrent.fi3_username)

ListBox1.Items.Add("dwIndex=" & dwIndex)
Next
End If
End Sub

<DllImport("netapi32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function NetFileEnum(ByVal servername As String, ByVal basepath As String, ByVal username As String, ByVal level As Integer, ByRef bufptr As IntPtr, ByVal prefmaxlen As Integer, _
ByRef entriesread As Integer, ByRef totalentries As Integer, ByVal resume_handle As IntPtr) As Integer
End Function

<DllImport("Netapi32.dll", SetLastError:=True)> _
Private Shared Function NetApiBufferFree(ByVal Buffer As IntPtr) As Integer
End Function

<structlayout(layoutkind.sequential, charset:="CharSet.Auto)"> _
Private Structure FILE_INFO_3
Public fi3_id As Integer
Public fi3_permission As Integer
Public fi3_num_locks As Integer
<marshalas(unmanagedtype.lpwstr)> _
Public fi3_pathname As String
<marshalas(unmanagedtype.lpwstr)> _
Public fi3_username As String
End Structure

End Class

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900