-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinder.cs
169 lines (120 loc) · 5.64 KB
/
Finder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace FindFilesFast;
public static class Finder
{
//https://docs.microsoft.com/en-us/windows/win32/fileio/file-attribute-constants
private static readonly uint FILE_ATTRIBUTE_DIRECTORY = 0x10;
private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
private static readonly uint FILE_ATTRIBUTE_HIDDEN = 0x2;
private static readonly uint FILE_ATTRIBUTE_REPARSE_POINT = 0x400;
private static readonly uint FILE_ATTRIBUTE_SYSTEM = 0x4;
// private static readonly int FILE_ATTRIBUTE_READONLY = 0x1;
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal 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;
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr FindFirstFile(String lpFileName, out WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool FindNextFile(IntPtr hFindFile, out WIN32_FIND_DATA lpFindFileData);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool FindClose(IntPtr hFindFile);
public struct FindFileResult
{
public uint Attributes { get; }
public DateTimeOffset CreationTime { get; }
public DateTimeOffset LastAccessTime { get; }
public DateTimeOffset LastWriteTime { get; }
public long FileSize { get; }
public string FileName { get; }
public string FullName { get; }
public string AlternateFileName { get; }
public bool IsDirectory { get; }
public bool IsReparsePoint { get; }
public bool IsSystem { get; }
public bool IsHidden { get; }
private static long IntsToLong(uint high, uint low)
=> (long)high << 32 | low;
private static long IntsToLong(int high, int low)
=> IntsToLong((uint)high, (uint)low);
private static DateTimeOffset DateTimeFromFileTime(System.Runtime.InteropServices.ComTypes.FILETIME fileTime)
=> DateTimeOffset.FromFileTime(IntsToLong(fileTime.dwHighDateTime, fileTime.dwLowDateTime));
// internal FindFileResult(string debug)
// {
// this.FullName = debug;
// }
internal FindFileResult(WIN32_FIND_DATA win32file, string parentDirectory)
{
this.Attributes = win32file.dwFileAttributes;
this.CreationTime = DateTimeFromFileTime(win32file.ftCreationTime);
this.LastAccessTime = DateTimeFromFileTime(win32file.ftLastAccessTime);
this.LastWriteTime = DateTimeFromFileTime(win32file.ftLastWriteTime);
this.FileSize = IntsToLong(win32file.nFileSizeHigh, win32file.nFileSizeLow);
this.FileName = win32file.cFileName;
this.AlternateFileName = win32file.cAlternateFileName;
this.IsDirectory = (win32file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) > 0;
this.IsReparsePoint = (win32file.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) > 0;
this.IsSystem = (win32file.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM) > 0;
this.IsHidden = (win32file.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) > 0;
this.FullName = parentDirectory + "\\" + this.FileName;
}
}
public static IEnumerable<FindFileResult> FindFiles(string directory, bool recurse = false, bool recurseOnReparse = false)
{
// yield return new FindFileResult($"DIR: {directory} RECURSE: {recurse}");
bool IsRecurse(FindFileResult currentResult)
=> recurse
&& currentResult.IsDirectory
// && !currentResult.IsHidden
// && !currentResult.IsSystem
&& (!currentResult.IsReparsePoint || recurseOnReparse);
bool IsSkip(FindFileResult currentResult)
=> (currentResult.FileName == ".")
|| (currentResult.FileName == "..");
var searchPointer = FindFirstFile(directory + "\\*", out var currentFile);
if (searchPointer == INVALID_HANDLE_VALUE)
{
yield break;
}
try
{
do
{
var currentResult = new FindFileResult(currentFile, directory);
if (IsSkip(currentResult))
{
continue;
}
yield return currentResult;
var isRecurse = IsRecurse(currentResult);
// yield return new FindFileResult($"shall we recurse? {isRecurse}");
if (isRecurse)
{
foreach (var subfolderResult in FindFiles(currentResult.FullName, recurse))
{
yield return subfolderResult;
}
}
}
while (FindNextFile(searchPointer, out currentFile));
}
finally
{
FindClose(searchPointer);
}
}
}