取文件的大小 (KB,MB,GB...)

取文件的大小 (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

调用方式:

  dim sFileFullName as String ="your file full name"
  dim iFileSize as Integer = New System.IO.FileInfo(sFileFullName ).Length / 1024
  dim sFileSize as String  

  sFileSize = GetFileSize(iFileSize) 

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;
        }

调用方式:

 string sFileFullName ="your file full name";
 string sFileSize = GetFileSize(sFileFullName );


其它:


            string sTEST = @"C:\part1\2014\201405\aa.txt";
            string s1 = System.IO.Path.GetFileName(sTEST); // get short file name
            string s2 = System.IO.Directory.GetParent(sTEST).FullName; // get folder

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值