国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#基于Extension Method(擴展方法)獲得文件大小的方法

2020-01-24 01:43:57
字體:
來源:轉載
供稿:網(wǎng)友

本文實例講述了C#基于Extension Method(擴展方法)獲得文件大小的方法。分享給大家供大家參考。具體分析如下:

文件信息類的一個Extension Method,返回文件大小的格式化的版本。
比如:1 GB or 100 B and it at max it will have two decimals.

添加下面代碼到同樣的命名空間的公共靜態(tài)類,創(chuàng)建新的FileInfo,調用GetFileSize。

/// <summary>/// Gets a files formatted size./// </summary>/// <param name="file">The file to return size of.</param>/// <returns></returns>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}

希望本文所述對大家的C#程序設計有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 邮箱| 隆尧县| 慈溪市| 马公市| 黄梅县| 赣榆县| 祥云县| 关岭| 年辖:市辖区| 嘉黎县| 泗水县| 徐汇区| 无极县| 平原县| 科技| 佛山市| 陇南市| 绵阳市| 务川| 琼结县| 临江市| 金华市| 咸丰县| 武威市| 平远县| 屏东县| 探索| 河池市| 肥乡县| 中江县| 额敏县| 云阳县| 阿巴嘎旗| 怀集县| 连云港市| 铅山县| 扬州市| 隆德县| 毕节市| 永定县| 织金县|