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

findfirstfileex (kernel32)
 
.
Summary

C# Signature:

    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern IntPtr FindFirstFileEx(
        string lpFileName,
        FINDEX_INFO_LEVELS fInfoLevelId,
        out WIN32_FIND_DATA lpFindFileData,
        FINDEX_SEARCH_OPS fSearchOp,
        IntPtr lpSearchFilter,
        int dwAdditionalFlags);

VB.NET Signature:

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
    Private Function FindFirstFileEx(ByVal lpFileName As String, ByVal fInfoLevelId As FINDEX_INFO_LEVELS, ByRef lpFindFileData As WIN32_FIND_DATA, ByVal fSearchOp As FINDEX_SEARCH_OPS, lpSearchFilter As Int32, dwAdditionalFlags As Integer) As Int32
    End Function

OBS
The corrent VB signature is:

   <DllImport("kernel32.dll", CharSet:=CharSet.Unicode)> _
    Public Shared Function FindFirstFileExW(ByVal lpFileName As String, ByVal fInfoLevelId As FINDEX_INFO_LEVELS, ByRef lpFindFileData As WIN32_FIND_DATAW, ByVal fSearchOp As FINDEX_SEARCH_OPS, lpSearchFilter As IntPtr, dwAdditionalFlags As Integer) As IntPtr
    End Function

Those 'int32's are IntPtr - and using an Int32 insteade will fail on a 64-bit platform.

User-Defined Types:

FINDEX_INFO_LEVELS

FINDEX_SEARCH_OPS

WIN32_FIND_DATA

// dwAdditionalFlags:
public const int FIND_FIRST_EX_CASE_SENSITIVE= 1;
public const int FIND_FIRST_EX_LARGE_FETCH = 2;

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    WIN32_FIND_DATA findData;
    FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoStandard;
    int additionalFlags = 0;
    if (Environment.OSVersion.Version.Major >= 6)
    {
    findInfoLevel = FINDEX_INFO_LEVELS.FindExInfoBasic;
    additionalFlags = FIND_FIRST_EX_LARGE_FETCH;
    }

    IntPtr hFile = FindFirstFileEx(
    pattern,
    findInfoLevel,
    out findData,
    FINDEX_SEARCH_OPS.FindExSearchNameMatch,
    IntPtr.Zero,
    additionalFlags);
    int error = Marshal.GetLastWin32Error();

    if (hFile.ToInt32() != -1)
    {
    do
    {
        if ((findData.dwFileAttributes & FileAttributes.Directory) != FileAttributes.Directory)
        {
        Console.WriteLine("Found file {0}", findData.cFileName);
        }
    }
    while (FindNextFile(hFile, out findData));

    FindClose(hFile);
    }

Alternative Managed API:

The FileInfo() class provides managed access to this information. As with all .NET file handling (as of framework 4) it is constrained in terms of the length of filename allowed.

Documentation

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