• C#实现全盘扫描,找到符合要求的文件,并把路径写入到TXT中


    .NET兼职社区

    好久没更新文章了,来水一篇。

    最近领导安排了个任务,让把服务器上符合要求的文件的路径找出来。windos自带的搜索功能太慢了,everything不想下载,想着自己实现一个,实现完成发现速度也不是很慢。

    实现思路。
    1.先把电脑中的所有驱动器名称拿到。
    2.遍历启动器中的文件。
    3.符合要求的文件写入文本。

    第一步中查了下资料,C#提供的有个DriveInfo的类,可以获取到所有驱动器相关的信息。

    第二步,遍历文件,这个一看就是费脑力的活,自己写估计效率很低,这个直接网上参考了国外大佬实现的类,直接用就行了。遍历速度和everything差不多。

    第三步,写入txt。这个就很简单了。不过也是CV。

    下面贴出具体实现代码,代码有注释。就不具体解释了。

    1.程序入口

    using System;
    using System.IO;
    using System.Linq;
    
    namespace 文件扫描器
    {
        class Program
        {
            static void Main(string[] args)
            {
                //全盘扫描
                DriveInfo[] driveInfo = DriveInfo.GetDrives();
    
                foreach (DriveInfo info in driveInfo)
                {
                    var MFTfile = new DriveInfo(info.Name).EnumerateFiles();//获得C盘所有文件路径
                    var FindFiles = MFTfile.Where(x =>
    
                       Path.GetFileName(x) == "大写.config" || Path.GetFileName(x) == "小写.config"
    
                    );//遍历获得的全部文件路径,查找所需的文件
                    foreach (string strpath in FindFiles)
                    {
                        WriteText.WriteDocument(strpath);
                    }
    
                }
    
    
                Console.WriteLine("执行成功!");
                Console.Read();
            }
        }
    }
    
    
    • 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

    2.遍历文件的类。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Security.Principal;
    using System.Text;
    
    namespace 文件扫描器
    {
    
    
    public class MFTScanner
        {
            private static IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
            private const uint GENERIC_READ = 0x80000000;
            private const int FILE_SHARE_READ = 0x1;
            private const int FILE_SHARE_WRITE = 0x2;
            private const int OPEN_EXISTING = 3;
            private const int FILE_READ_ATTRIBUTES = 0x80;
            private const int FILE_NAME_IINFORMATION = 9;
            private const int FILE_FLAG_BACKUP_SEMANTICS = 0x2000000;
            private const int FILE_OPEN_FOR_BACKUP_INTENT = 0x4000;
            private const int FILE_OPEN_BY_FILE_ID = 0x2000;
            private const int FILE_OPEN = 0x1;
            private const int OBJ_CASE_INSENSITIVE = 0x40;
            private const int FSCTL_ENUM_USN_DATA = 0x900b3;
    
            [StructLayout(LayoutKind.Sequential)]
            private struct MFT_ENUM_DATA
            {
                public long StartFileReferenceNumber;
                public long LowUsn;
                public long HighUsn;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct USN_RECORD
            {
                public int RecordLength;
                public short MajorVersion;
                public short MinorVersion;
                public long FileReferenceNumber;
                public long ParentFileReferenceNumber;
                public long Usn;
                public long TimeStamp;
                public int Reason;
                public int SourceInfo;
                public int SecurityId;
                public FileAttributes FileAttributes;
                public short FileNameLength;
                public short FileNameOffset;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct IO_STATUS_BLOCK
            {
                public int Status;
                public int Information;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct UNICODE_STRING
            {
                public short Length;
                public short MaximumLength;
                public IntPtr Buffer;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            private struct OBJECT_ATTRIBUTES
            {
                public int Length;
                public IntPtr RootDirectory;
                public IntPtr ObjectName;
                public int Attributes;
                public int SecurityDescriptor;
                public int SecurityQualityOfService;
            }
    
             MFT_ENUM_DATA
            [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
            private static extern bool DeviceIoControl(IntPtr hDevice, int dwIoControlCode, ref MFT_ENUM_DATA lpInBuffer, int nInBufferSize, IntPtr lpOutBuffer, int nOutBufferSize, ref int lpBytesReturned, IntPtr lpOverlapped);
    
            [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
            private static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, int dwShareMode, IntPtr lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, IntPtr hTemplateFile);
    
            [DllImport("kernel32.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
            private static extern Int32 CloseHandle(IntPtr lpObject);
    
            [DllImport("ntdll.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
            private static extern int NtCreateFile(ref IntPtr FileHandle, int DesiredAccess, ref OBJECT_ATTRIBUTES ObjectAttributes, ref IO_STATUS_BLOCK IoStatusBlock, int AllocationSize, int FileAttribs, int SharedAccess, int CreationDisposition, int CreateOptions, int EaBuffer,
            int EaLength);
    
            [DllImport("ntdll.dll", ExactSpelling = true, SetLastError = true, CharSet = CharSet.Auto)]
            private static extern int NtQueryInformationFile(IntPtr FileHandle, ref IO_STATUS_BLOCK IoStatusBlock, IntPtr FileInformation, int Length, int FileInformationClass);
    
            private IntPtr m_hCJ;
            private IntPtr m_Buffer;
            private int m_BufferSize;
    
            private string m_DriveLetter;
    
            private class FSNode
            {
                public long FRN;
                public long ParentFRN;
                public string FileName;
    
                public bool IsFile;
    
                public FSNode(long lFRN, long lParentFSN, string sFileName, bool bIsFile)
                {
                    FRN = lFRN;
                    ParentFRN = lParentFSN;
                    FileName = sFileName;
                    IsFile = bIsFile;
                }
            }
    
            private IntPtr OpenVolume(string szDriveLetter)
            {
                IntPtr hCJ = default(IntPtr);
                 volume handle
    
                m_DriveLetter = szDriveLetter;
                hCJ = CreateFile(@"\\.\" + szDriveLetter, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
    
                return hCJ;
            }
    
            private void Cleanup()
            {
                if (m_hCJ != IntPtr.Zero)
                {
                    // Close the volume handle.
                    CloseHandle(m_hCJ);
                    m_hCJ = INVALID_HANDLE_VALUE;
                }
    
                if (m_Buffer != IntPtr.Zero)
                {
                    // Free the allocated memory
                    Marshal.FreeHGlobal(m_Buffer);
                    m_Buffer = IntPtr.Zero;
                }
            }
    
            public IEnumerable EnumerateFiles(string szDriveLetter)
            {
                try
                {
                    var usnRecord = default(USN_RECORD);
                    var mft = default(MFT_ENUM_DATA);
                    var dwRetBytes = 0;
                    var cb = 0;
                    var dicFRNLookup = new Dictionary();
                    var bIsFile = false;
    
                    // This shouldn't be called more than once.
                    if (m_Buffer.ToInt32() != 0)
                    {
                        throw new Exception("invalid buffer");
                    }
    
                    // Assign buffer size
                    m_BufferSize = 65536;
                    //64KB
    
                    // Allocate a buffer to use for reading records.
                    m_Buffer = Marshal.AllocHGlobal(m_BufferSize);
    
                    // correct path
                    szDriveLetter = szDriveLetter.TrimEnd('\\');
    
                    // Open the volume handle
                    m_hCJ = OpenVolume(szDriveLetter);
    
                    // Check if the volume handle is valid.
                    if (m_hCJ == INVALID_HANDLE_VALUE)
                    {
                        string errorMsg = "Couldn't open handle to the volume.";
                        if (!IsAdministrator())
                            errorMsg += "Current user is not administrator";
                    }
    
                    mft.StartFileReferenceNumber = 0;
                    mft.LowUsn = 0;
                    mft.HighUsn = long.MaxValue;
    
                    do
                    {
                        if (DeviceIoControl(m_hCJ, FSCTL_ENUM_USN_DATA, ref mft, Marshal.SizeOf(mft), m_Buffer, m_BufferSize, ref dwRetBytes, IntPtr.Zero))
                        {
                            cb = dwRetBytes;
                            // Pointer to the first record
                            IntPtr pUsnRecord = new IntPtr(m_Buffer.ToInt64() + 8);
    
                            while ((dwRetBytes > 8))
                            {
                                // Copy pointer to USN_RECORD structure.
                                usnRecord = (USN_RECORD)Marshal.PtrToStructure(pUsnRecord, usnRecord.GetType());
    
                                // The filename within the USN_RECORD.
                                string FileName = Marshal.PtrToStringUni(new IntPtr(pUsnRecord.ToInt64() + usnRecord.FileNameOffset), usnRecord.FileNameLength / 2);
    
                                bIsFile = !usnRecord.FileAttributes.HasFlag(FileAttributes.Directory);
                                dicFRNLookup.Add(usnRecord.FileReferenceNumber, new FSNode(usnRecord.FileReferenceNumber, usnRecord.ParentFileReferenceNumber, FileName, bIsFile));
    
                                // Pointer to the next record in the buffer.
                                pUsnRecord = new IntPtr(pUsnRecord.ToInt64() + usnRecord.RecordLength);
    
                                dwRetBytes -= usnRecord.RecordLength;
                            }
    
                            // The first 8 bytes is always the start of the next USN.
                            mft.StartFileReferenceNumber = Marshal.ReadInt64(m_Buffer, 0);
                        }
                        else
                        {
                            break; // TODO: might not be correct. Was : Exit Do
                        }
                    } while (!(cb <= 8));
    
                    // Resolve all paths for Files
                    foreach (FSNode oFSNode in dicFRNLookup.Values.Where(o => o.IsFile))
                    {
                        string sFullPath = oFSNode.FileName;
                        FSNode oParentFSNode = oFSNode;
    
                        while (dicFRNLookup.TryGetValue(oParentFSNode.ParentFRN, out oParentFSNode))
                        {
                            sFullPath = string.Concat(oParentFSNode.FileName, @"\", sFullPath);
                        }
                        sFullPath = string.Concat(szDriveLetter, @"\", sFullPath);
    
                        yield return sFullPath;
                    }
                }
                finally
                {
                     cleanup
                    Cleanup();
                }
            }
    
            public static bool IsAdministrator()
            {
                WindowsIdentity identity = WindowsIdentity.GetCurrent();
                WindowsPrincipal principal = new WindowsPrincipal(identity);
                return principal.IsInRole(WindowsBuiltInRole.Administrator);
            }
        }
    
        public static class DriveInfoExtension
        {
            public static IEnumerable<String> EnumerateFiles(this DriveInfo drive)
            {
                return (new MFTScanner()).EnumerateFiles(drive.Name);
            }
        }
    }
    
    
    • 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
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263

    该类需要使用引用using System.Security.Principal;自己Nuget一下。

    3.写入txt类。

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 文件扫描器
    {
        class WriteText
        {
            public static void WriteDocument(string  strpath) 
            {
             
                using (System.IO.StreamWriter file =
                  new System.IO.StreamWriter(@"C:\WriteLines.txt", true))
                {
                 
                        file.WriteLine(strpath);
                  
                }
            }
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    全网课程下载 🐧:3388486286
    好了,今天文章已水完~希望大家有收获。

  • 相关阅读:
    Python函数进阶
    【无标题】
    超纯水制备
    JWT 弊端解决思路(主动过期\权限更新)
    C# 观察者模式
    ArrayList、LinkedList和Vector的区别
    压缩包密码可以删除吗?
    SkeyeVSS果园防盗智能监控系统 保障果农收益好帮手
    华为OD 字符串消除(100分)【java】A卷+B卷
    ThreadLocal模板
  • 原文地址:https://blog.csdn.net/csdn2990/article/details/128148279