最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net c# 获得文件大小各种方法总结
时间:2022-06-25 08:19:52 编辑:袖梨 来源:一聚教程网
文件信息类的一个Extension Method,返回文件大小的格式化的版本。比如:1 GB or 100 B and it at max it will have two decimals.添加下面代码到同样的命名空间的公共静态类,创建新的FileInfo,调用GetFileSize。
C# 获取文件大小,创建时间,文件信息,FileInfo类的属性表 OpenFileDialog openFileDialog1 = new OpenFileDialog();
代码如下 | 复制代码 |
if(openFileDialog1.ShowDialog() == DialogResult.OK) file.Name;//文件名 |
获取大小并转换单位MB
代码如下 | 复制代码 |
/// /// Gets a files formatted size. /// /// The file to return size of. /// public static string GetFileSize(this FileInfo file) { try { //determine all file sizes double sizeinbytes = file.Length; double sizeinkbytes = Math.Round((sizeinbytes / 1024)); double sizeinmbytes = Math.Round((sizeinkbytes / 1024)); double sizeingbytes = Math.Round((sizeinmbytes / 1024)); if (sizeingbytes > 1) return string.Format("{0} GB", sizeingbytes); //returns size in gigabytes else if (sizeinmbytes > 1) return string.Format("{0} MB", sizeinmbytes); //returns size in megabytes if less than one gigabyte else if (sizeinkbytes > 1) return string.Format("{0} KB", sizeinkbytes); //returns size in kilabytes if less than one megabyte else return string.Format("{0} B", sizeinbytes); //returns size in bytes if less than one kilabyte } catch { return "Error Getting Size"; } //catches any possible error and just returns error getting size } |
获取文件夹及文件占用空间
首先通过windows API获取磁盘的相关信息。
//调用windows API获取磁盘空闲空间
//导入库
代码如下 | 复制代码 |
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
/// |
相关文章
- 模拟山羊3抱树者位置在哪-模拟山羊3抱树者在哪里 09-18
- 明日方舟2025新春前瞻直播多久开始-2025新春前瞻直播开启时间介绍 09-18
- 纳萨力克之王护盾队怎么配队-纳萨力克之王护盾队配队攻略 09-18
- 七日世界月之预兆异常物有哪些-月之预兆版本新增异常物一览 09-18
- 王者荣耀2024FMVP皮肤是谁的-2024FMVP皮肤介绍 09-18
- 鸣潮赞妮武器选什么-鸣潮赞妮武器推荐 09-18