Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

NetFileEnum (netapi32)
 
.
Summary
The NetFileEnum function returns information about some or all open files on a server, depending on the parameters specified

C# Signature:

[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
);

VB Signature:

Declare Unicode Function NetFileEnum Lib "netapi32.dll" ( _
    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

User-Defined Types:

FILE_INFO_3

Notes:

Definition from the API

    'NET_API_STATUS NetFileEnum(
    '  LPWSTR servername,
    '  LPWSTR basepath,
    '  LPWSTR username,
    '  DWORD level,
    '  LPBYTE* bufptr,
    '  DWORD prefmaxlen,
    '  LPDWORD entriesread,
    '  LPDWORD totalentries,
    '  PDWORD_PTR resume_handle
    ');

Tips & Tricks:

You must free the memory allocated to the bufptr with the NetApiBufferFree function

C# Sample Code:

    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);

    }

VB.Net Sample Code:

    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)
        Next
     end if

Alternative Managed API:

Do you know one? Please contribute it!

Documentation
NetFileEnum on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions