123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System;
- using System.ComponentModel;
- using System.IO;
- using System.Runtime.InteropServices;
- namespace QVCopier.Utility.FindFile
- {
- internal static class FindFileApi
- {
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
- private static extern IntPtr FindFirstFileEx(
- string lpFileName,
- FINDEX_INFO_LEVELS fInfoLevelId,
- out WIN32_FIND_DATA lpFindFileData,
- FINDEX_SEARCH_OPS fSearchOp,
- IntPtr lpSearchFilter,
- int dwAdditionalFlags);
- [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
- private static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA lpFindFileData);
- [DllImport("kernel32.dll")]
- private static extern bool FindClose(IntPtr hFindFile);
- // dwAdditionalFlags:
- private const int FIND_FIRST_EX_CASE_SENSITIVE = 1;
- private const int FIND_FIRST_EX_LARGE_FETCH = 2;
- public static void Find(string fileNameOrPath)
- {
- 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;
- }
- var hFile = FindFirstFileEx(
- fileNameOrPath,
- findInfoLevel,
- out var findData,
- FINDEX_SEARCH_OPS.FindExSearchNameMatch,
- IntPtr.Zero,
- additionalFlags);
- int error = Marshal.GetLastWin32Error();
- if (error == 2)
- {
- hFile = FindFirstFileEx(
- Path.Combine(fileNameOrPath, "*.*"),
- findInfoLevel,
- out findData,
- FINDEX_SEARCH_OPS.FindExSearchNameMatch,
- IntPtr.Zero,
- additionalFlags);
- }
- if (hFile.ToInt32() != -1)
- {
- do
- {
- if ((findData.dwFileAttributes & FileAttributes.Directory) != FileAttributes.Directory)
- {
- Console.WriteLine("File {0}", findData.cFileName);
- }
- else
- {
- Console.WriteLine("Dir {0}", findData.cFileName);
- }
- }
- while (FindNextFile(hFile, out findData));
- FindClose(hFile);
- }
- else
- {
- var ex = new Win32Exception();
- }
- }
- }
- }
|