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#判斷程序是否是管理員權(quán)限運(yùn)行的方法代碼示例
這篇文章主要介紹了C#判斷程序是否是管理員權(quán)限運(yùn)行的方法代碼示例,本文直接給出實(shí)現(xiàn)代碼例子,需要的朋友可以參考下2015-03-03
Unity接入百度AI實(shí)現(xiàn)貨幣識(shí)別
本文主要介紹了在Unity中接入百度AI,從而實(shí)現(xiàn)貨幣識(shí)別,可以返回貨幣的名稱、代碼、面值、年份信息等,感興趣的可以跟隨小編學(xué)習(xí)一下2022-01-01
C# TreeView無(wú)限目錄樹(shù)實(shí)現(xiàn)方法
這篇文章主要介紹了C# TreeView無(wú)限目錄樹(shù)實(shí)現(xiàn)方法,實(shí)例分析了TreeView節(jié)點(diǎn)操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
WPF+ASP.NET SignalR實(shí)現(xiàn)后臺(tái)通知功能的示例代碼
本文以一個(gè)簡(jiǎn)單示例,簡(jiǎn)述如何通過(guò)WPF+ASP.NET SignalR實(shí)現(xiàn)消息后臺(tái)通知以及數(shù)據(jù)的實(shí)時(shí)刷新,僅供學(xué)習(xí)分享使用,如有不足之處,還請(qǐng)指正2022-09-09
Unity3D UGUI實(shí)現(xiàn)縮放循環(huán)拖動(dòng)卡牌展示效果
這篇文章主要為大家詳細(xì)介紹了Unity3D UGUI實(shí)現(xiàn)縮放循環(huán)拖動(dòng)展示卡牌效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
Unity調(diào)取移動(dòng)端的麥克風(fēng)進(jìn)行錄音并播放
這篇文章主要為大家詳細(xì)介紹了Unity調(diào)取移動(dòng)端的麥克風(fēng)進(jìn)行錄音并播放,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06

