最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
asp.net c# 获得文件大小各种方法总结
时间:2013-05-22 编辑:简简单单 来源:一聚教程网
文件信息类的一个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)]
/// |
-
上一个: asp.net C#解压缩/文件程序代码
相关文章
- C#复制数组的两种方式及效率比较解读 10-24
- ASP.NET Identity用法解析 10-24
- ASP.NET MVC使用Identity增删改查用户介绍 10-24
- C语言中atoi函数模拟实现介绍 10-18
- .Net反向代理组件Yarp用法介绍 10-10
- .NET使用YARP通过编码方式配置域名转发实现反向代理教程 10-10