C#中獲取文件大小問題
C# 獲取文件大小
直接貼代碼吧
? ? ? ? /// <summary> ? ? ? ? /// 格式化文件大小 ? ? ? ? /// </summary> ? ? ? ? /// <param name="filesize">文件傳入大小</param> ? ? ? ? /// <returns></returns> ? ? ? ? private static string GetFileSize(long filesize) ? ? ? ? { ? ? ? ? ? ? try ? ? ? ? ? ? { ? ? ? ? ? ? ? ? if (filesize < 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? return "0"; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (filesize >= 1024 * 1024 * 1024) ?//文件大小大于或等于1024MB ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? return string.Format("{0:0.00} GB", (double)filesize / (1024 * 1024 * 1024)); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (filesize >= 1024 * 1024) //文件大小大于或等于1024KB ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? return string.Format("{0:0.00} MB", (double)filesize / (1024 * 1024)); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else if (filesize >= 1024) //文件大小大于等于1024bytes ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? return string.Format("{0:0.00} KB", (double)filesize / 1024); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? return string.Format("{0:0.00} bytes", filesize); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? ? ? catch (Exception ex) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? throw ex; ? ? ? ? ? ? } ? ? ? ? ? }
上述代碼是將文件大小格式化為我們想要的大小。
?FileInfo t = new FileInfo(filePath);//獲取文件 ?文件大小 = GetFileSize(t.Length);//這樣我們就獲取到了文件的大小
C# 獲取文件占用空間 (絕對準確)
先說一下為什么要用這種極其麻煩的方法來判斷文件的占用空間,因為找不到簡單的方法。
如果是想算文件夾的占用空間,只需要將里面的文件的占用空間加在一起就可以了。
首先說下文件大小與占用空間的區(qū)別
這與是硬盤分區(qū)格式有關。
大小是文件的實際大小,而占用空間是占硬盤的實際空間,以FAT32格式為例,硬盤的基本存儲單位是簇,在FAT32中一簇是4KB 那么,也就是說即使文件只有1個字節(jié),在硬盤上也要占到4KB的空間 如果文件是4KB零1個字節(jié),那就要占用8KB的空間,以此類推 結論: 大小是文件的實際大小,而占用空間是占硬盤的實際空間。
如圖(我這里一簇是4kB)
計算思路
所以,要想獲得占用空間,就需要先獲得文件的大小,然后就可以通過把簇補全即可算出文件的占用空間。而獲取文件大小的方法很簡單,其代碼如下。
FileInfo fileInfo = new FileInfo(filePath); Console.WriteLine(fileInfo.Length);
但是通過這種方法計算出的數(shù)據(jù)并不準確
為什么會不準確呢?因為有很多不正常的文件,那些文件的大小是大于文件占用空間的,例如:
而這種情況通過上面的那一段代碼求出的文件大小為23677字節(jié),然后補全簇之后得出的結果一定是大于文件大小的,怎么也不可能得出8192字節(jié)(8KB),所以,通過這種方法得出的結果是不準確的。
為什么會出現(xiàn)這種情況?根據(jù)硬盤存儲空間的規(guī)則可以得出,占用空間一定是比其文件大小要大的。那么,只有一種可能,那就是該大小并不是文件的實際大小,它是假的(也有可能是文件管理系統(tǒng)中的某個未知的壓縮功能導致的)。
獲取文件的實際大小
要想獲取一個文件的實際大小,需要調用底層的windows API,這些api都是通過C++來編寫的。
里面就有一個可以用來獲取文件的實際大小:GetCompressedFileSize()方法。
該方法的說明文檔如下:(為什么里面的方法名多了個A,我也不知道為什么,反正可以拿來用)
https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getcompressedfilesizea
所以獲取文件實際大小的方法如下:
static void Main(string[] args) { string name = @"D:\Documents\test.zip"; //用來獲取高位數(shù)字(只有在讀取超過4GB的文件才需要用到該參數(shù)) uint h = 0; //用來獲取低位數(shù)據(jù) uint l = GetCompressedFileSize(name, ref h); //將兩個int32拼接成一個int64 ulong r = ((ulong)h << 32) + l; FileInfo fileInfo = new FileInfo(name); Console.WriteLine(fileInfo.Length); Console.WriteLine(h); Console.WriteLine(l); //最終結果 Console.WriteLine(r); Console.ReadKey(true); } [DllImport("Kernel32.dll", CharSet = CharSet.Auto)] private static extern uint GetCompressedFileSize(string fileName, ref uint fileSizeHigh);
然后拿一個正常的文件測試一下
可以看出,字節(jié)數(shù)是正確的,然后再加上補全簇的算法,一切就正常了。
其代碼如下:
static void Main(string[] args) { string name = @"C:\Windows\DiagTrack\GetFileActionAllowedList.dat"; //string name = @"D:\Documents\test.zip"; uint h = 0; uint l = GetCompressedFileSize(name, ref h); ulong r = ((ulong)h << 32) + l; FileInfo fileInfo = new FileInfo(name); Console.WriteLine(fileInfo.Length); Console.WriteLine(h); Console.WriteLine(l); Console.WriteLine(r); ulong size = GetClusterSize("D:\\"); if (r%size != 0) { decimal res = r / size; uint clu = (uint)Convert.ToInt32(Math.Ceiling(res)) + 1; r = size * clu; } //最終結果 Console.WriteLine(r); Console.ReadKey(true); } //獲取每簇的字節(jié)數(shù) private static uint GetClusterSize(string rootPath) { //提前聲明各項參數(shù) uint sectorsPerCluster = 0, bytesPerSector = 0, numberOfFreeClusters = 0, totalNumberOfClusters = 0; GetDiskFreeSpace(rootPath, ref sectorsPerCluster, ref bytesPerSector, ref numberOfFreeClusters, ref totalNumberOfClusters); return bytesPerSector * sectorsPerCluster; } //用于獲取文件實際大小的api [DllImport("Kernel32.dll", CharSet = CharSet.Auto)] private static extern uint GetCompressedFileSize(string fileName, ref uint fileSizeHigh); //用于獲取盤信息的api [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern bool GetDiskFreeSpace([MarshalAs(UnmanagedType.LPTStr)]string rootPathName, ref uint sectorsPerCluster, ref uint bytesPerSector, ref uint numberOfFreeClusters, ref uint totalNumbeOfClusters);
最后再看一下那個不正常的文件:
結果8192字節(jié),計算成功。
。。。
。。。
。。。
這個C#也太坑了吧,為了弄一個獲取占用空間,我搞了整整一整天。也不知道微軟怎么想的,就不能直接給一個獲取占用空間方法嗎?非地讓我們自己算,哎。
后續(xù)
有時間為上面的代碼加了一點說明,同時也加上了錯誤處理,修改后的代碼如下:
using System; using System.ComponentModel; using System.IO; using System.Runtime.InteropServices; namespace ConsoleApp1 { class Program { static void Main(string[] args) { string name = @"目標文件夾路徑"; uint h = 0; uint l = GetCompressedFileSize(name, ref h); if (l == uint.MaxValue) throw new IOException("文件讀取失敗。", new Win32Exception(Marshal.GetLastWin32Error())); ulong r = ((ulong)h << 32) + l; FileInfo fileInfo = new FileInfo(name); Console.WriteLine("文件大?。?); Console.WriteLine(fileInfo.Length); Console.WriteLine("高位數(shù)據(jù):"); Console.WriteLine(h); Console.WriteLine("低位數(shù)據(jù):"); Console.WriteLine(l); Console.WriteLine("文件實際大?。?); Console.WriteLine(r); ulong size = GetClusterSize("D:\\"); if (r % size != 0) { decimal res = r / size; uint clu = (uint)Convert.ToInt32(Math.Ceiling(res)) + 1; r = size * clu; } //最終結果 Console.WriteLine("文件占用空間:"); Console.WriteLine(r); Console.ReadKey(true); } //獲取每簇的字節(jié)數(shù) private static uint GetClusterSize(string rootPath) { //提前聲明各項參數(shù) uint sectorsPerCluster = 0, bytesPerSector = 0, numberOfFreeClusters = 0, totalNumberOfClusters = 0; GetDiskFreeSpace(rootPath, ref sectorsPerCluster, ref bytesPerSector, ref numberOfFreeClusters, ref totalNumberOfClusters); return bytesPerSector * sectorsPerCluster; } //用于獲取文件實際大小的api [DllImport("Kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] private static extern uint GetCompressedFileSize(string fileName, ref uint fileSizeHigh); //用于獲取盤信息的api [DllImport("kernel32.dll", CharSet = CharSet.Auto)] private static extern bool GetDiskFreeSpace([MarshalAs(UnmanagedType.LPTStr)]string rootPathName, ref uint sectorsPerCluster, ref uint bytesPerSector, ref uint numberOfFreeClusters, ref uint totalNumbeOfClusters); } }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
C#實現(xiàn)的Windows剪貼板監(jiān)視器功能實例【附demo源碼下載】
這篇文章主要介紹了C#實現(xiàn)的Windows剪貼板監(jiān)視器功能,結合實例形式分析了C#實現(xiàn)剪貼板監(jiān)視功能所涉及的相關Windows API函數(shù)與使用技巧,需要的朋友可以參考下2016-08-08.net2.0+ Winform項目實現(xiàn)彈出容器層
在實際工作中,如果能像菜單一樣彈出自定義內容,會方便很多,比如查詢時,比如下拉列表顯示多列信息時,比如在填寫某個信息需要查看一些信息樹時。這個時候自定義彈出界面就顯的非常重要了2015-08-08一文詳解C#中重寫(override)及覆蓋(new)的區(qū)別
這篇文章主要為大家詳細介紹了C#中重寫(override)及覆蓋(new)這兩個關鍵詞的區(qū)別,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-03-03