namespace CommonLibrary.SystemUtils { /// <summary> /// 表示存储空间大小计量单位 /// </summary> public enum SizeUnitMode { /// <summary> /// 字节 /// </summary> Byte, /// <summary> /// 千字节 /// </summary> KiloByte, /// <summary> /// 兆字节 /// </summary> MegaByte, /// <summary> /// 亿字节 /// </summary> GigaByte, }//end enum SizeUnitMode }//end namespace CommonLibrary.SystemUtils
#region 磁盘空间计算 ///<summary> ///获取指定驱动器上存储空间的总大小(以字节为单位) /// <example>示例1:获取C盘上存储空间的总大小 /// Int64 totalSize=CommonLibrary.SystemUtils.DiskUtil.GetTotalSize("c"); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetTotalSize() /// { /// Int64 totalSize = CommonLibrary.SystemUtils.DiskUtil.GetTotalSize("c"); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> ///</summary> ///<param name="driveName">表示驱动器标识的字母,如C、D、E、F等。注意:它可以是从“a”到“z”的大写或小写字母。Null 值无效。</param> ///<returns>以字节为单位的驱动器上存储空间的总大小</returns> public static System.Int64 GetTotalSize(string driveName) { System.Int64 totalSize = 0; totalSize = System.Convert.ToInt64(GetTotalSize(driveName, SizeUnitMode.Byte)); return totalSize; }//end method ///<summary> ///获取指定驱动器上存储空间的总大小 /// <example>示例1:获取C盘上存储空间的总大小,小数形式更加精确 /// double totalSizesizeMB = CommonLibrary.SystemUtils.DiskUtil.GetTotalSize("c", CommonLibrary.SystemUtils.SizeUnitMode.MegaByte); /// double totalSizesizeGB = CommonLibrary.SystemUtils.DiskUtil.GetTotalSize("c", CommonLibrary.SystemUtils.SizeUnitMode.GigaByte); /// double totalSizesizeB = CommonLibrary.SystemUtils.DiskUtil.GetTotalSize("c", CommonLibrary.SystemUtils.SizeUnitMode.Byte); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetTotalSize() /// { /// double totalSizesizeMB = CommonLibrary.SystemUtils.DiskUtil.GetTotalSize("c", CommonLibrary.SystemUtils.SizeUnitMode.MegaByte); /// double totalSizesizeGB = CommonLibrary.SystemUtils.DiskUtil.GetTotalSize("c", CommonLibrary.SystemUtils.SizeUnitMode.GigaByte); /// double totalSizesizeB = CommonLibrary.SystemUtils.DiskUtil.GetTotalSize("c", CommonLibrary.SystemUtils.SizeUnitMode.Byte); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> ///</summary> ///<param name="driveName">表示驱动器标识的字母,如C、D、E、F等。注意:它可以是从“a”到“z”的大写或小写字母。Null 值无效。</param> ///<param name="sizeUnitMode">计算空间时使用的单位</param> ///<returns>以指定单位表示的驱动器上存储空间的总大小</returns> public static System.Double GetTotalSize(string driveName,SizeUnitMode sizeUnitMode) { System.Double totalSize = 0; driveName = driveName + ":" + System.IO.Path.DirectorySeparatorChar; System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives(); foreach (System.IO.DriveInfo drive in drives) { if (drive.Name.ToLower() == driveName.ToLower()) { totalSize = drive.TotalSize; } } switch (sizeUnitMode) { case SizeUnitMode.MegaByte: totalSize = totalSize / (1024 * 1024 ); break; case SizeUnitMode.GigaByte: totalSize = totalSize / (1024 * 1024 * 1024); break; default : break; }//end switch return totalSize; }//end method ///<summary> ///获取指定驱动器上剩余空间的总大小(以字节为单位) /// <example>示例1:获取C盘上剩余空间的总大小 /// Int64 freeSize = CommonLibrary.SystemUtils.DiskUtil.GetFreeSpaceSize("c"); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetFreeSpaceSize() /// { /// Int64 freeSize = CommonLibrary.SystemUtils.DiskUtil.GetFreeSpaceSize("c"); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> ///</summary> ///<param name="driveName">表示驱动器标识的字母,如C、D、E、F等</param> ///<returns>以字节为单位的驱动器上剩余空间的总大小</returns> public static long GetFreeSpaceSize(string driveName) { System.Int64 freeSpace=0; freeSpace = System.Convert.ToInt64(GetFreeSpaceSize(driveName, SizeUnitMode.Byte)); return freeSpace; }//end method ///<summary> /// 获取指定驱动器上剩余空间的总大小 /// <example>示例1:获取C盘上剩余空间的总大小,小数形式更加精确 /// double freeSizeMB = CommonLibrary.SystemUtils.DiskUtil.GetFreeSpaceSize("c", CommonLibrary.SystemUtils.SizeUnitMode.MegaByte); /// double freeSizeGB = CommonLibrary.SystemUtils.DiskUtil.GetFreeSpaceSize("c", CommonLibrary.SystemUtils.SizeUnitMode.GigaByte); /// double freeSizeB = CommonLibrary.SystemUtils.DiskUtil.GetFreeSpaceSize("c", CommonLibrary.SystemUtils.SizeUnitMode.Byte); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetFreeSpaceSize() /// { /// double freeSizeMB = CommonLibrary.SystemUtils.DiskUtil.GetFreeSpaceSize("c", CommonLibrary.SystemUtils.SizeUnitMode.MegaByte); /// double freeSizeGB = CommonLibrary.SystemUtils.DiskUtil.GetFreeSpaceSize("c", CommonLibrary.SystemUtils.SizeUnitMode.GigaByte); /// double freeSizeB = CommonLibrary.SystemUtils.DiskUtil.GetFreeSpaceSize("c", CommonLibrary.SystemUtils.SizeUnitMode.Byte); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> ///</summary> ///<param name="driveName">表示驱动器标识的字母,如C、D、E、F等。注意:它可以是从“a”到“z”的大写或小写字母。Null 值无效。</param> ///<param name="sizeUnitMode">计算空间时使用的单位</param> ///<returns>以指定单位表示的驱动器上剩余空间的总大小</returns> public static System.Double GetFreeSpaceSize(string driveName, SizeUnitMode sizeUnitMode) { System.Int64 freeSpace = 0; driveName = driveName + ":" + System.IO.Path.DirectorySeparatorChar; System.IO.DriveInfo[] drives = System.IO.DriveInfo.GetDrives(); foreach (System.IO.DriveInfo drive in drives) { if (drive.Name.ToLower() == driveName.ToLower()) { freeSpace = drive.TotalFreeSpace; } } //单位换算 switch (sizeUnitMode) { case SizeUnitMode.MegaByte: freeSpace = freeSpace / (1024 * 1024); break; case SizeUnitMode.GigaByte: freeSpace = freeSpace / (1024 * 1024 * 1024); break; default: break; }//end switch return freeSpace; }//end method #endregion 磁盘空间计算
#region 目录空间计算 /// <summary> /// 获取指定目录占用的存储空间的总大小(以字节为单位) /// <example>示例1:获取 C:\Resource 目录占用的存储空间的总大小 /// Int64 directorySize = CommonLibrary.SystemUtils.DiskUtil.GetDirectorySize(@"C:\Resource"); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetDirectorySize() /// { /// Int64 directorySize = CommonLibrary.SystemUtils.DiskUtil.GetDirectorySize(@"C:\Resource"); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> /// </summary> /// <param name="directoryPath">表示目录的标识,如 C:\myDirectory 等</param> /// <returns>以字节为单位的目录占用的存储空间的总大小</returns> public static System.Int64 GetDirectorySize(string directoryPath) { System.Int64 size = 0; size = System.Convert.ToInt64(GetDirectorySize(directoryPath, SizeUnitMode.Byte)); return size; }//end method ///<summary> ///获取指定目录占用的存储空间的总大小,小数形式更加精确 /// <example>示例1:获取 C:\Resource 目录占用的存储空间的总大小 /// double directorySizeMB = CommonLibrary.SystemUtils.DiskUtil.GetDirectorySize(@"C:\Resource", CommonLibrary.SystemUtils.SizeUnitMode.MegaByte); /// double directorySizeGB = CommonLibrary.SystemUtils.DiskUtil.GetDirectorySize(@"C:\Resource", CommonLibrary.SystemUtils.SizeUnitMode.GigaByte); /// double directorySizeBB = CommonLibrary.SystemUtils.DiskUtil.GetDirectorySize(@"C:\Resource", CommonLibrary.SystemUtils.SizeUnitMode.Byte); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetDirectorySize() /// { /// double directorySizeMB = CommonLibrary.SystemUtils.DiskUtil.GetDirectorySize(@"C:\Resource", CommonLibrary.SystemUtils.SizeUnitMode.MegaByte); /// double directorySizeGB = CommonLibrary.SystemUtils.DiskUtil.GetDirectorySize(@"C:\Resource", CommonLibrary.SystemUtils.SizeUnitMode.GigaByte); /// double directorySizeBB = CommonLibrary.SystemUtils.DiskUtil.GetDirectorySize(@"C:\Resource", CommonLibrary.SystemUtils.SizeUnitMode.Byte); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> ///</summary> ///<param name="directoryPath">表示目录的标识,如 C:\myDirectory 等</param> ///<param name="sizeUnitMode">计算空间时使用的单位</param> ///<returns>以指定单位表示的目录占用的存储空间的总大小</returns> public static System.Double GetDirectorySize(string directoryPath, SizeUnitMode sizeUnitMode) { System.Double size = 0; size = GetDirectorySizeDouble(directoryPath); //单位换算 switch (sizeUnitMode) { case SizeUnitMode.MegaByte: size = size / (1024 * 1024); break; case SizeUnitMode.GigaByte: size = size / (1024 * 1024 * 1024); break; default: break; }//end switch return size; }//end method ///<summary> ///获取指定目录占用的存储空间的总大小(以字节为单位) /// <example>示例1:获取 C:\Resource 目录占用的存储空间的总大小 /// Int64 directorySize = CommonLibrary.SystemUtils.DiskUtil.GetDirectorySizeDouble(@"C:\Resource"); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetDirectorySizeDouble() /// { /// Int64 directorySize = CommonLibrary.SystemUtils.DiskUtil.GetDirectorySizeDouble(@"C:\Resource"); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> ///</summary> ///<param name="directoryPath">表示目录的标识,如 C:\myDirectory 等</param> ///<returns>目录占用的存储空间的总大小</returns> private static System.Double GetDirectorySizeDouble(string directoryPath) { System.Double size = 0; System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(directoryPath); //计算当前目录中所有文件的总大小 System.IO.FileInfo[] fileInfoList = directoryInfo.GetFiles(); foreach (System.IO.FileInfo f in fileInfoList) { size += f.Length; } //计算子目录大小 System.IO.DirectoryInfo[] directoryInfoList = directoryInfo.GetDirectories(); foreach (System.IO.DirectoryInfo d in directoryInfoList) { size += GetDirectorySizeDouble(d.FullName); } return size; }//end method #endregion 目录空间计算
#region 文件空间计算 //<summary> ///获取指定文件占用的存储空间的总大小,返回值小数精确且带单位 /// <example>示例1:获取 C:\Resource\file1.txt 文件占用的存储空间的总大小 /// String fileSize = CommonLibrary.SystemUtils.FileUtil.GetFileSize(@"C:\Resource\file1.txt"); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetFileSize() /// { /// /// String fileSize = CommonLibrary.SystemUtils.FileUtil.GetFileSize(@"C:\Resource\file1.txt"); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> ///</summary> ///<param name="fileFullPath">表示文件的标识,如 C:\Resource\file1.txt 等</param> ///<returns>以特定单位表示的文件占用的存储空间的总大小</returns> public static System.String GetFileSizeString(string fileFullPath) { System.Double size = 0; CommonLibrary.SystemUtils.SizeUnitMode sizeUnitMode; size = GetFileSize(fileFullPath, out sizeUnitMode); string result = string.Empty; //单位换算 switch (sizeUnitMode) { case SizeUnitMode.Byte: result = System.String.Format("{0} B", size.ToString("N", System.Globalization.CultureInfo.InvariantCulture)); break; case SizeUnitMode.KiloByte: result = System.String.Format("{0} KB", size.ToString("N", System.Globalization.CultureInfo.InvariantCulture)); break; case SizeUnitMode.MegaByte: result = System.String.Format("{0} MB", size.ToString("N", System.Globalization.CultureInfo.InvariantCulture)); break; case SizeUnitMode.GigaByte: result = System.String.Format("{0} G", size.ToString("N", System.Globalization.CultureInfo.InvariantCulture)); break; default: break; }//end switch return result; }//end method ///<summary> ///获取指定文件占用的存储空间的总大小,返回计算单位,小数精确 /// <example>示例1:获取 C:\Resource\file1.txt 文件占用的存储空间的总大小 /// CommonLibrary.SystemUtils.SizeUnitMode unitMode=CommonLibrary.SystemUtils.SizeUnitMode.MegaByte; /// double fileSize = CommonLibrary.SystemUtils.FileUtil.GetFileSize(@"C:\Resource\file1.txt", out unitMode); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetFileSize() /// { /// CommonLibrary.SystemUtils.SizeUnitMode unitMode=CommonLibrary.SystemUtils.SizeUnitMode.MegaByte; /// double fileSize = CommonLibrary.SystemUtils.FileUtil.GetFileSize(@"C:\Resource\file1.txt", out unitMode); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> ///</summary> ///<param name="fileFullPath">表示文件的标识,如 C:\Resource\file1.txt 等</param> ///<param name="sizeUnitMode">计算空间时使用的单位</param> ///<returns>以指定单位表示的文件占用的存储空间的总大小</returns> public static System.Double GetFileSize(string fileFullPath, out SizeUnitMode sizeUnitMode) { System.Double size = 0; size = GetFileSizeDouble(fileFullPath); if (size >= 0 && size < 1024) { sizeUnitMode = SizeUnitMode.Byte; return size; } System.Double kb = size / 1024; if (kb >= 1 && kb < 1024) { sizeUnitMode = SizeUnitMode.KiloByte; return kb; } System.Double mb = size / (1024 * 1024); if (mb >= 1 && mb < 1024) { sizeUnitMode = SizeUnitMode.MegaByte; return mb; } System.Double gb = size / (1024 * 1024 * 1024); if (gb >= 1) { sizeUnitMode = SizeUnitMode.GigaByte; return gb; } sizeUnitMode = SizeUnitMode.Byte; return size; }//end method /// <summary> /// 获取指定文件占用的存储空间的总大小(以字节为单位) /// <example>示例1:获取 C:\Resource\file1.txt 占用的存储空间的总大小 /// Int64 fileSize = CommonLibrary.SystemUtils.FileUtil.GetFileSize(@"C:\Resource\file1.txt"); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetFileSize() /// { /// Int64 fileSize = CommonLibrary.SystemUtils.FileUtil.GetFileSize(@"C:\Resource\file1.txt"); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> /// </summary> /// <param name="fileFullPath">表示文件的标识,如 C:\Resource\file1.txt 等</param> /// <returns>以字节为单位的文件占用的存储空间的总大小</returns> public static System.Int64 GetFileSize(string fileFullPath) { System.Int64 size = 0; size = System.Convert.ToInt64(GetFileSize(fileFullPath, SizeUnitMode.Byte)); return size; }//end method ///<summary> ///获取指定文件占用的存储空间的总大小,小数形式更加精确 /// <example>示例1:获取 C:\Resource\file1.txt 文件占用的存储空间的总大小 /// double fileSizeMB = CommonLibrary.SystemUtils.FileUtil.GetFileSize(@"C:\Resource\file1.txt", CommonLibrary.SystemUtils.SizeUnitMode.MegaByte); /// double fileSizeGB = CommonLibrary.SystemUtils.FileUtil.GetFileSize(@"C:\Resource\file1.txt", CommonLibrary.SystemUtils.SizeUnitMode.GigaByte); /// double fileSizeBB = CommonLibrary.SystemUtils.FileUtil.GetFileSize(@"C:\Resource\file1.txt", CommonLibrary.SystemUtils.SizeUnitMode.Byte); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetFileSize() /// { /// double fileSizeMB = CommonLibrary.SystemUtils.FileUtil.GetFileSize((@"C:\Resource\file1.txt", CommonLibrary.SystemUtils.SizeUnitMode.MegaByte); /// double fileSizeGB = CommonLibrary.SystemUtils.FileUtil.GetFileSize((@"C:\Resource\file1.txt", CommonLibrary.SystemUtils.SizeUnitMode.GigaByte); /// double fileSizeBB = CommonLibrary.SystemUtils.FileUtil.GetFileSize((@"C:\Resource\file1.txt", CommonLibrary.SystemUtils.SizeUnitMode.Byte); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> ///</summary> ///<param name="fileFullPath">表示文件的标识,如 C:\Resource\file1.txt 等</param> ///<param name="sizeUnitMode">计算空间时使用的单位</param> ///<returns>以指定单位表示的文件占用的存储空间的总大小</returns> public static System.Double GetFileSize(string fileFullPath, SizeUnitMode sizeUnitMode) { System.Double size = 0; size = GetFileSizeDouble(fileFullPath); //单位换算 switch (sizeUnitMode) { case SizeUnitMode.MegaByte: size = size / (1024 * 1024); break; case SizeUnitMode.GigaByte: size = size / (1024 * 1024 * 1024); break; default: break; }//end switch return size; }//end method ///<summary> ///获取指定文件占用的存储空间的总大小(以字节为单位) /// <example>示例1:获取 C:\Resource\file1.txt 文件占用的存储空间的总大小 /// Int64 fileSize = CommonLibrary.SystemUtils.FileUtil.GetFileSizeDouble(@"C:\Resource\file1.txt"); /// <code> /// <![CDATA[ /// class MyClass /// { /// //模拟 /// public void TestGetFileSizeDouble() /// { /// Int64 fileSize = CommonLibrary.SystemUtils.FileUtil.GetFileSizeDouble(@"C:\Resource\file1.txt"); /// /// }//end method /// }//end class /// ]]> /// </code> /// </example> ///</summary> ///<param name="directoryPath">表示文件的标识,如 C:\Resource\file1.txt 等</param> ///<returns>文件占用的存储空间的总大小</returns> private static System.Double GetFileSizeDouble(string fileFullPath) { System.Double size = 0; System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileFullPath); //计算当前目录中所有文件的总大小 size = fileInfo.Length; return size; }//end method #endregion 文件空间计算