網站首頁 編程語言 正文
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# 獲取文件占用空間 (絕對準確)
先說一下為什么要用這種極其麻煩的方法來判斷文件的占用空間,因為找不到簡單的方法。
如果是想算文件夾的占用空間,只需要將里面的文件的占用空間加在一起就可以了。
首先說下文件大小與占用空間的區別
這與是硬盤分區格式有關。
大小是文件的實際大小,而占用空間是占硬盤的實際空間,以FAT32格式為例,硬盤的基本存儲單位是簇,在FAT32中一簇是4KB 那么,也就是說即使文件只有1個字節,在硬盤上也要占到4KB的空間 如果文件是4KB零1個字節,那就要占用8KB的空間,以此類推 結論: 大小是文件的實際大小,而占用空間是占硬盤的實際空間。
如圖(我這里一簇是4kB)
計算思路
所以,要想獲得占用空間,就需要先獲得文件的大小,然后就可以通過把簇補全即可算出文件的占用空間。而獲取文件大小的方法很簡單,其代碼如下。
FileInfo fileInfo = new FileInfo(filePath);
Console.WriteLine(fileInfo.Length);
但是通過這種方法計算出的數據并不準確
為什么會不準確呢?因為有很多不正常的文件,那些文件的大小是大于文件占用空間的,例如:
而這種情況通過上面的那一段代碼求出的文件大小為23677字節,然后補全簇之后得出的結果一定是大于文件大小的,怎么也不可能得出8192字節(8KB),所以,通過這種方法得出的結果是不準確的。
為什么會出現這種情況?根據硬盤存儲空間的規則可以得出,占用空間一定是比其文件大小要大的。那么,只有一種可能,那就是該大小并不是文件的實際大小,它是假的(也有可能是文件管理系統中的某個未知的壓縮功能導致的)。
獲取文件的實際大小
要想獲取一個文件的實際大小,需要調用底層的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";
//用來獲取高位數字(只有在讀取超過4GB的文件才需要用到該參數)
uint h = 0;
//用來獲取低位數據
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);
然后拿一個正常的文件測試一下
可以看出,字節數是正確的,然后再加上補全簇的算法,一切就正常了。
其代碼如下:
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);
}
//獲取每簇的字節數
private static uint GetClusterSize(string rootPath)
{
//提前聲明各項參數
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字節,計算成功。
。。。
。。。
。。。
這個C#也太坑了吧,為了弄一個獲取占用空間,我搞了整整一整天。也不知道微軟怎么想的,就不能直接給一個獲取占用空間方法嗎?非地讓我們自己算,哎。
后續
有時間為上面的代碼加了一點說明,同時也加上了錯誤處理,修改后的代碼如下:
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("高位數據:");
Console.WriteLine(h);
Console.WriteLine("低位數據:");
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);
}
//獲取每簇的字節數
private static uint GetClusterSize(string rootPath)
{
//提前聲明各項參數
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);
}
}
總結
原文鏈接:https://blog.csdn.net/qq_38370387/article/details/110858565
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2023-01-05 Pandas使用Merge與Join和Concat分別進行合并數據效率對比分析_python
- 2022-05-27 對Entity?Framework?Core進行單元測試_實用技巧
- 2023-01-29 使用Python統計代碼運行時間的兩種方法_python
- 2022-07-09 systemd開機啟動和關機回調腳本
- 2022-06-08 記錄linux ens33網卡啟動失敗的問題
- 2022-06-01 idea對CPU的占用率過大問題的解決方法_相關技巧
- 2022-07-25 Redisson如何解決Redis分布式鎖提前釋放問題_Redis
- 2022-11-27 網站https訪問是443端口還是433端口_服務器其它
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支