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