1

How would I go about calling Win32 API for long file paths, the only thing I want to do is get a list of all the files in that directory (recursivly)

Steven
  • 13,250
  • 33
  • 95
  • 147

1 Answers1

1

If you want to use Win32 calls you will first have to use DllImport to import the kernel, the syntax is like something like this and you must do this for every method you want to use(this is all un-tested pseudo-code that only describes the concept), the code example converts your paths to UNC paths so you can have long file paths:

    using Microsoft.Win32.SafeHandles;
    ...
    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
            static extern SafeHandleMinusOneIsInvalid FindFirstFileW(string lpFileName, IntPtr lpFindFileData);

    ...

            public String FindFirstFile(string filepath)
            {
                // If file path is disk file path then prepend it with \\?\
                // if file path is UNC prepend it with \\?\UNC\ and remove \\ prefix in unc path.
                if (filepath.StartsWith(@"\\"))
                    filepath = @"\\?\UNC\" + filepath.Substring(2, filepath.Length - 2);
                else
                    filepath = @"\\?\" + filepath;
...
                SafeHandleMinusOneIsInvalid ret = FindFirstFileW(filepath, lpFindFileData);
...
            }

After you call FindFirstFile you must call FindNextFile for the next file in the directory, and then finally FindClose; for a complete example about how to list files in a directory using the Win32 kernel look here