Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
216 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...