簡體   English   中英

處理文件屬性C#

[英]Dealing with file properties C#

我想使用C#獲取任何給定文件的文件大小,並且如果可能的話,我需要以GB,MB,KB和字節為單位顯示它...

對於音頻(mp3)文件,我需要獲取文件的持續時間...

您可以使用FileInfo.Length來獲取文件的大小(以字節為單位)。 然后簡單的計算就可以告訴您KB,MB和GB:

string fileName = "C:\Path\to\file.txt";
var fileInfo = new FileInfo(fileName);

Console.WriteLine("Length = {0} bytes", fileInfo.Length);
Console.WriteLine("      or {0} KB", fileInfo.Length / 1024);
Console.WriteLine("      or {0} MB", fileInfo.Length / 1024 / 1024);
Console.WriteLine("      or {0} GB", fileInfo.Length / 1024 / 1024 / 1024);

要獲取mp3文件的持續時間,您需要使用一個支持讀取mp3文件標題的庫(例如TagLib# )來解析持續時間。

嘗試這個:

public class FileSizeFormatProvider : IFormatProvider, ICustomFormatter
    {
        public object GetFormat(Type formatType)
        {
            if (formatType == typeof(ICustomFormatter)) return this;
            return null;
        }

        private const string fileSizeFormat = "fs";
        private const Decimal OneKiloByte = 1024M;
        private const Decimal OneMegaByte = OneKiloByte * 1024M;
        private const Decimal OneGigaByte = OneMegaByte * 1024M;

        public string Format(string format, object arg, IFormatProvider formatProvider)
        {
            if (format == null || !format.StartsWith(fileSizeFormat))
            {
                return defaultFormat(format, arg, formatProvider);
            }

            if (arg is string)
            {
                return defaultFormat(format, arg, formatProvider);
            }

            Decimal size;

            try
            {
                size = Convert.ToDecimal(arg);
            }
            catch (InvalidCastException)
            {
                return defaultFormat(format, arg, formatProvider);
            }

            string suffix;
            if (size > OneGigaByte)
            {
                size /= OneGigaByte;
                suffix = "GB";
            }
            else if (size > OneMegaByte)
            {
                size /= OneMegaByte;
                suffix = "MB";
            }
            else if (size > OneKiloByte)
            {
                size /= OneKiloByte;
                suffix = "kB";
            }
            else
            {
                suffix = " B";
            }

            string precision = format.Substring(2);
            if (String.IsNullOrEmpty(precision)) precision = "2";
            return String.Format("{0:N" + precision + "}{1}", size, suffix);

        }

        private static string defaultFormat(string format, object arg, IFormatProvider formatProvider)
        {
            IFormattable formattableArg = arg as IFormattable;
            if (formattableArg != null)
            {
                return formattableArg.ToString(format, formatProvider);
            }
            return arg.ToString();
        }

    }

在主類中這樣稱呼它:

public static void Main()
        {
            FileInfo fInfo = new FileInfo(@"D:\Songs\housefull01(www.songs.pk).mp3");
  Console.WriteLine(String.Format(new FileSizeFormatProvider(), "File size: {0:fs}", fInfo.Length));
        }

確定文件大小:

FileInfo fileInfo = new FileInfo(fileName);
Console.WriteLine("Length = " + fileInfo.Length.toString());

您將必須根據需要/需要轉換該值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM