這篇“如何用C#代碼實(shí)現(xiàn)快速查詢文件”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“如何用C#代碼實(shí)現(xiàn)快速查詢文件”文章吧。
十載的巢湖網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。全網(wǎng)整合營(yíng)銷推廣的優(yōu)勢(shì)是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整巢湖建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)從事“巢湖網(wǎng)站設(shè)計(jì)”,“巢湖網(wǎng)站推廣”以來,每個(gè)客戶項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。
相信使用過Everything的人都對(duì)其超快的搜索速度印象非常深刻,它的主要原理是通過掃描NTFS磁盤的USN Journal讀取的文件列表,而不是磁盤目錄,由于USN Journal非常小,因此能實(shí)現(xiàn)快速搜索。
由于.Net程序的Dll基本上是通用的,在C#中也可以直接使用它。
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<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) { string errorMsg = "Couldn't open handle to the volume."; if (!IsAdministrator()) errorMsg += "Current user is not administrator"; throw new Exception(errorMsg); } 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(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.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(); } } public static bool IsAdministrator() { WindowsIdentity identity = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(identity); return principal.IsInRole(WindowsBuiltInRole.Administrator); } }
本文還提供了一個(gè)擴(kuò)展方法,方便我們獲取某個(gè)磁盤下的所有的文件名。
public static class DriveInfoExtension { public static IEnumerable<String> EnumerateFiles(this DriveInfo drive) { return (new MFTScanner()).EnumerateFiles(drive.Name); } }
需要注意的是,讀取USN Journal是需要管理員權(quán)限的,因此使用這個(gè)類需要管理員權(quán)限才能正常運(yùn)行。
另外,這個(gè)類封裝的也略為簡(jiǎn)單,只讀取了文件名,實(shí)際上還可以讀取文件大小,屬性等常用信息,修改一下代碼非常容易獲取這些屬性。通過它們可以非常方便寫出一些分析磁盤空間占用的程序,這里就不舉例了。
以上就是關(guān)于“如何用C#代碼實(shí)現(xiàn)快速查詢文件”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞標(biāo)題:如何用C#代碼實(shí)現(xiàn)快速查詢文件
文章起源:http://jinyejixie.com/article40/iehjho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站、網(wǎng)站導(dǎo)航、做網(wǎng)站、ChatGPT、定制開發(fā)、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)