本文只针对NTFS格式化的磁盘文件快速检索,速度不是非常快,是让你震惊。

一般用文件遍历的方法检索一个50G的文件夹需要几十分钟甚至一个小时的时间,而用本方法只需几秒。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.VisualBasic; 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 FileAttribute 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<String> 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<long, FSNode>();
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)
{
throw new Exception("Couldn't open handle to the volume.");
} 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.ToInt32() + 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.ToInt32() + usnRecord.FileNameOffset), usnRecord.FileNameLength / 2); bIsFile = !usnRecord.FileAttributes.HasFlag(FileAttribute.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.ToInt32() + 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();
}
}
}

调用方法:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics; namespace ConsoleApplication28
{
class Program
{
static void Main(string[] args)
{
var sw = Stopwatch.StartNew();
var files = EnumerateFiles("f:").ToArray();
var elapsed = sw.ElapsedMilliseconds.ToString();
Console.WriteLine(string.Format("Found {0} files, elapsed {1} ms", files.Length, elapsed));
}
}
}

方法只能传磁盘名

C#快速找出磁盘内的所有文件的更多相关文章

  1. 如何快速找出Linux中的重复文件

    md5sum | sort | uniq -w32 --all-repeated=separate [1]find -not -empty -type f -printf “%s\n” :find是查 ...

  2. Matlab.NET混合编程技巧之——找出Matlab内置函数

    原文:[原创]Matlab.NET混合编程技巧之--找出Matlab内置函数 Matlab与.NET的混合编程,掌握了基本过程,加上一定的开发经验和算法基础,肯定不难.反之,有时候一个小错误,可能抓破 ...

  3. 【原创】Matlab.NET混合编程技巧之找出Matlab内置函数

                  本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新    Matlab和C#混合编程文章目录 :[目录]Matlab和C#混合编程文章目录 Matlab与.N ...

  4. Matlab.NET混编技巧之——找出Matlab内置函数

    原文 http://www.cnblogs.com/asxinyu/p/3295309.html Matlab与.NET的混合编程,掌握了基本过程,加上一定的开发经验和算法基础,肯 定不难.反之,有时 ...

  5. python——快速找出两个电子表中数据的差异

    最近刚接触python,找点小任务来练练手,希望自己在实践中不断的锻炼自己解决问题的能力. 公司里会有这样的场景:有一张电子表格的内容由两三个部门或者更多的部门用到,这些员工会在维护这些表格中不定期的 ...

  6. 快速找出网站中可能存在的XSS漏洞实践

    笔者写了一些XSS漏洞的挖掘过程记录下来,方便自己也方便他人. 一.背景 在本篇文章当中会一permeate生态测试系统为例,笔者此前写过一篇文章当中笔者已经讲解如何安装permeate渗透测试系统, ...

  7. 快速找出网站中可能存在的XSS漏洞实践(一)

    一.背景 笔者最近在慕课录制了一套XSS跨站漏洞 加强Web安全视频教程,课程当中有讲到XSS的挖掘方式,所以在录制课程之前需要做大量实践案例,最近视频已经录制完成,准备将这些XSS漏洞的挖掘过程记录 ...

  8. 快速找出故障机器(single number)

    简单起见,假设每个机器存储一个标号为ID的记录(ID是小于十亿的整数),假设每份数据都保存两个备份,这样就有两个机器储存了同样的数据. 1.在某个时间,如果得到一个数据文件ID的列表,是否能够快速地找 ...

  9. 编程之美 set 12 快速找出故障机器

    题目 1. 所有的 ID 都出现 2 次, 只有一个例外, 找到那个例外的 ID 2. 所有的 ID 都出现两次, 只有两个例外, 找出例外的那两个 总计 1. 剑指 offer 上有这两道题的解法, ...

随机推荐

  1. Javascript中与Scroll有关的方法

    这块确实太乱了,被兼容搞的简直快要晕死,默默地总结下... 与scroll相关的方法 4个window对象下:scrollX.scrollY.scrollTo.scroll(作用和scrollTo一样 ...

  2. 【51Nod】1055 最长等差数列 动态规划

    [题目]1055 最长等差数列 [题意]给定大小为n的互不不同正整数集合,求最长等差数列的长度.\(n \leq 10000\). [算法]动态规划 两个数之间的差是非常重要的信息,设\(f_{i,j ...

  3. [R语言]关联规则1---不考虑items之间的时序关系

    本文介绍的是关联规则,分为两部分:第一部分是---不考虑用户购买的items之间严格的时序关系,每个用户有一个“购物篮”,查找其中的关联规则.第二部分--- 考虑items之间的严格的时序关系来分析用 ...

  4. 20165230 ch02 课上测试

    题目一 1.参考附图代码,编写一个程序 "week0201学号.c",判断一下你的电脑是大端还是小端. 2.提交运行结果"学号XXXX的笔记本电脑是X端"的运行 ...

  5. 【干货】Windows系统信息收集篇

    市场分析:计算机取证,就是应急响应.而应急响应的市场在于黑产的攻击频率.在当今的社会里,更多的人为了钱铤而走险的比比皆是,这个市场随着比特币,大数据,物联网的来临,规模将更加的庞大与有组织性.这将导致 ...

  6. Socket心跳包机制总结【转】

    转自:https://blog.csdn.net/qq_23167527/article/details/54290726 跳包之所以叫心跳包是因为:它像心跳一样每隔固定时间发一次,以此来告诉服务器, ...

  7. 为什么要使用断路器Hystrix?

    为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自身的原因,服务并不能保证服 ...

  8. java iterator

    list l = new ArrayList(); l.add("aa"); l.add("bb"); l.add("cc"); for ( ...

  9. MySQL的聚集索引和非聚集索引

    一. MYSQL的索引 mysql中,不同的存储引擎对索引的实现方式不同,大致说下MyISAM和InnoDB两种存储引擎. MyISAM的B+Tree的叶子节点上的data,并不是数据本身,而是数据存 ...

  10. 喊山 BFS

    一个山头呼喊的声音可以被临近的山头同时听到.题目假设每个山头最多有两个能听到它的临近山头.给定任意一个发出原始信号的山头,本题请你找出这个信号最远能传达到的地方. 输入格式: 输入第一行给出3个正整数 ...