处理文件属性 C#

发布于 2024-09-12 18:07:04 字数 107 浏览 2 评论 0原文

我想使用 C# 获取任何给定文件的文件大小,如果可能的话,我需要以 GB、MB、KB 和字节为单位显示它......

对于音频 (mp3) 文件,我需要获取该文件的持续时间文件 ...

I want to get the file size of any given file using the C#, and I need to show it in GB, MB, KB and bytes if possible ...

and for audio (mp3) files, I need to get the duration of the file ...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱没勇气 2024-09-19 18:07:04

您可以使用 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 文件的持续时间,您需要使用库(例如 TagLib#) 支持读取 mp3 文件的标头以解析出持续时间。

You can use FileInfo.Length to get the size (in bytes) of a file. Then a simple calculation can tell you KB, MB and 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);

To get the duration of an mp3 file, you'll need to use a library (such as TagLib#) that supports reading the headers of the mp3 file to parse out the duration.

一城柳絮吹成雪 2024-09-19 18:07:04

试试这个:

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

try this:

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

    }

In main class call it like this:

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));
        }
倾城月光淡如水﹏ 2024-09-19 18:07:04

确定文件大小:

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

您必须根据需要转换该值。

Determining the file size:

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

You will have to convert that value as you wish / need.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文