123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- using System;
- using System.ComponentModel;
- using System.IO;
- using System.Runtime.InteropServices;
- namespace Bsm.Core
- {
- public static class IoUtil
- {
- public static long GetFileSize(string filename)
- {
- FindClose(FindFirstFile(filename, out var findData));
- return (((long)findData.nFileSizeHigh) << 32) | findData.nFileSizeLow;
- }
- public static bool GetFileInfo(string path, out WIN32_FIND_DATA data)
- {
- var hFind = FindFirstFileEx(path, FINDEX_INFO_LEVELS.FindExInfoBasic, out var file, 0, IntPtr.Zero, 0);
- if (hFind == new IntPtr(INVALID_HANDLE_VALUE))
- {
- var ex = new Win32Exception();
- data = new WIN32_FIND_DATA();
- return false;
- }
- data = file;
- FindClose(hFind);
- return true;
- }
- public static void LookupEntries(string dir, Action<WIN32_FIND_DATA> callback, string pattern = "*")
- {
- var hFind = FindFirstFileEx(Path.Combine(dir, pattern), FINDEX_INFO_LEVELS.FindExInfoBasic, out var file, 0, IntPtr.Zero, 0);
- if (hFind == new IntPtr(INVALID_HANDLE_VALUE))
- {
- var ex = new Win32Exception();
- return;
- }
- try
- {
- do
- {
- //check is . or ..
- if (file.cFileName == "." || file.cFileName == "..") continue;
- callback(file);
- } while (FindNextFile(hFind, out file));
- }
- finally
- {
- FindClose(hFind);
- }
- }
- public static void LookupFilesRecursively(string dir, Action<WIN32_FIND_DATA, string> callback, string pattern = "*")
- {
- LookupFilesInternal(dir, callback, pattern: pattern);
- }
- private static void LookupFilesInternal(string dir, Action<WIN32_FIND_DATA, string> callback, string folderName = "", string pattern = "*")
- {
- var hFind = FindFirstFileEx(Path.Combine(dir, pattern), FINDEX_INFO_LEVELS.FindExInfoBasic, out var file, 0, IntPtr.Zero, 0);
- if (hFind == new IntPtr(INVALID_HANDLE_VALUE))
- {
- var ex = new Win32Exception();
- return;
- }
- try
- {
- do
- {
- //check is . or ..
- if (file.cFileName == "." || file.cFileName == "..") continue;
- //check is dir re
- if (file.IsDirectory)
- {
- LookupFilesInternal(Path.Combine(dir, file.cFileName), callback, folderName + "/" + file.cFileName, pattern);
- continue;
- }
- callback(file, folderName);
- } while (FindNextFile(hFind, out file));
- }
- finally
- {
- FindClose(hFind);
- }
- }
- // ReSharper disable IdentifierTypo
- // ReSharper disable InconsistentNaming
- // ReSharper disable MemberCanBePrivate.Local
- // ReSharper disable FieldCanBeMadeReadOnly.Local
- [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
- private static extern IntPtr FindFirstFile(string lpFileName, out WIN32_FIND_DATA lpFindFileData);
- [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);
- [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);
- public const int INVALID_HANDLE_VALUE = -1;
- public const uint FILE_ATTRIBUTE_DIRECTORY = 16;
- [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
- public struct WIN32_FIND_DATA
- {
- public uint dwFileAttributes;
- public System.Runtime.InteropServices.ComTypes.FILETIME ftCreationTime;
- public System.Runtime.InteropServices.ComTypes.FILETIME ftLastAccessTime;
- public System.Runtime.InteropServices.ComTypes.FILETIME ftLastWriteTime;
- public uint nFileSizeHigh;
- public uint nFileSizeLow;
- public uint dwReserved0;
- public uint dwReserved1;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
- public string cFileName;
- [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
- public string cAlternateFileName;
- public bool IsDirectory => 0 != (dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
- public long FileSize => (((long)nFileSizeHigh) << 32) | nFileSizeLow;
- public DateTime CreationTime
- {
- get
- {
- var high = (ulong)ftCreationTime.dwHighDateTime;
- var low = (uint)ftCreationTime.dwLowDateTime;
- var fileTime = (long)((high << 32) + low);
- return DateTime.FromFileTimeUtc(fileTime);
- }
- }
- public DateTime LastWriteTime
- {
- get
- {
- var high = (ulong)ftLastWriteTime.dwHighDateTime;
- var low = (uint)ftLastWriteTime.dwLowDateTime;
- var fileTime = (long)((high << 32) + low);
- return DateTime.FromFileTimeUtc(fileTime);
- }
- }
- }
- public enum FINDEX_INFO_LEVELS
- {
- FindExInfoStandard = 0,
- FindExInfoBasic = 1
- }
- public enum FINDEX_SEARCH_OPS
- {
- FindExSearchNameMatch = 0,
- FindExSearchLimitToDirectories = 1,
- FindExSearchLimitToDevices = 2
- }
- // ReSharper restore FieldCanBeMadeReadOnly.Local
- // ReSharper restore MemberCanBePrivate.Local
- // ReSharper restore InconsistentNaming
- // ReSharper restore IdentifierTypo
- }
- }
|