zoukankan      html  css  js  c++  java
  • 取文件的大小 (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:part12014201405aa.txt";
                string s1 = System.IO.Path.GetFileName(sTEST); // get short file name
                string s2 = System.IO.Directory.GetParent(sTEST).FullName; // get folder

  • 相关阅读:
    印刷行业合版BOM全阶维护示例
    C#实现WinForm禁止最大化、最小化、双击标题栏、双击图标等操作的方法
    EasyUI Tree节点拖动到指定容器
    Excel GET.DOCUMENT说明
    Excel GET.CELL说明
    ExecuteExcel4Macro (宏函数)使用说明
    MSSQL:查看所有触发器信息的命令
    SQL Server 2008作业失败:无法确定所有者是否有服务器访问权限
    Cocoa History
    Working with Methods
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4060386.html
Copyright © 2011-2022 走看看