标签:style class blog code color get
取文件的大小 (KB,MB,GB...)
2种方式: VB 和 C#
1, VB
Public Function GetFileSize(ByVal iFileSizeKB As Long) As String Dim iFileSizeMB As Integer Dim iFileSizeGB As Integer If (iFileSizeKB >= 1024) Then iFileSizeMB = iFileSizeKB / 1024 If (iFileSizeMB >= 1024) Then iFileSizeGB = iFileSizeMB / 1024 End If End If If (iFileSizeGB > 0) Then Return iFileSizeGB.ToString() + " GB" ElseIf (iFileSizeMB > 0) Then Return iFileSizeMB.ToString() + " MB" Else Return iFileSizeKB.ToString() + " KB" End If End Function
2, C#
public string GetFileSize(string sFileFullName) { FileInfo fiInput = new FileInfo(sFileFullName); double len = fiInput.Length; string[] sizes = { "B", "KB", "MB", "GB" }; int order = 0; while (len >= 1024 && order + 1 < sizes.Length) { order++; len = len / 1024; } string filesize = String.Format("{0:0.##} {1}", len, sizes[order]); return filesize; }
public static bool FileIsLargerThan1KB(string sFileFullName) { FileInfo fiInput = new FileInfo(sFileFullName); double len = fiInput.Length; len = len / 1024 / 1024; return len > 1; }
调用方式:
取文件的大小 (KB,MB,GB...),布布扣,bubuko.com
标签:style class blog code color get
原文地址:http://blog.csdn.net/keenweiwei/article/details/30083197