文件大小帮助类

public class SizeHelper
	{
		/// <summary>
		/// 获取文件大小并以B,KB,GB,TB方式表示
		/// </summary>
		/// <param name="file">文件(FileInfo类型)</param>
		/// <returns></returns>
		public static string GetFileSize(FileInfo file)
		{
			string result = "";
			long length = file.Length;
			bool flag = length >= 1073741824L;
			if (flag)
			{
				result = ((length / 1024L * 1024L * 1024L * 1024L >= 1024L) ? string.Format("{0:############0.00} TB", (double)length / 1024.0 * 1024.0 * 1024.0 * 1024.0) : string.Format("{0:####0.00} GB", (double)length / 1024.0 * 1024.0 * 1024.0));
			}
			else
			{
				bool flag2 = length >= 1048576L;
				if (flag2)
				{
					result = string.Format("{0:####0.00} MB", (double)length / 1024.0 * 1024.0);
				}
				else
				{
					bool flag3 = length >= 1024L;
					if (flag3)
					{
						result = string.Format("{0:####0.00} KB", (double)length / 1024.0);
					}
					else
					{
						result = string.Format("{0:####0.00} Bytes", length);
					}
				}
			}
			return result;
		}
		/// <summary>
		/// 获取文件大小并以B,KB,GB,TB方式表示
		/// </summary>
		/// <param name="filePath">文件的具体路径</param>
		/// <returns></returns>
		public static string GetFileSize(string filePath)
		{
			FileInfo fileInfo = new FileInfo(filePath);
			long length = fileInfo.Length;
			bool flag = length >= 1073741824L;
			string result;
			if (flag)
			{
				result = ((length / 1024L * 1024L * 1024L * 1024L >= 1024L) ? string.Format("{0:########0.00} TB", (double)length / 1024.0 * 1024.0 * 1024.0 * 1024.0) : string.Format("{0:####0.00} GB", (double)length / 1024.0 * 1024.0 * 1024.0));
			}
			else
			{
				bool flag2 = length >= 1048576L;
				if (flag2)
				{
					result = string.Format("{0:####0.00} MB", (double)length / 1024.0 * 1024.0);
				}
				else
				{
					bool flag3 = length >= 1024L;
					if (flag3)
					{
						result = string.Format("{0:####0.00} KB", (double)length / 1024.0);
					}
					else
					{
						result = string.Format("{0:####0.00} Bytes", length);
					}
				}
			}
			return result;
		}
		/// <summary>
		/// 计算文件大小函数(保留两位小数),Size为字节大小
		/// </summary>
		/// <param name="size">初始文件大小</param>
		/// <returns></returns>
		public static string CountSize(long size)
		{
			string result = "";
			long num = size;
			bool flag = (double)num < 1024.0;
			if (flag)
			{
				result = num.ToString("F2") + " 字节";
			}
			else
			{
				bool flag2 = (double)num >= 1024.0 && num < 1048576L;
				if (flag2)
				{
					double num2 = (double)num / 1024.0;
					result = num2.ToString("F2") + " KB";
				}
				else
				{
					bool flag3 = num >= 1048576L && num < 1073741824L;
					if (flag3)
					{
						double num2 = (double)num / 1024.0 / 1024.0;
						result = num2.ToString("F2") + " MB";
					}
					else
					{
						bool flag4 = num >= 1073741824L;
						if (flag4)
						{
							double num2 = (double)num / 1024.0 / 1024.0 / 1024.0;
							result = num2.ToString("F2") + " GB";
						}
					}
				}
			}
			return result;
		}
	}
posted @ 2022-07-26 20:13  码农阿亮  阅读(47)  评论(0编辑  收藏  举报