要读取文件的大小并将其转换为MB单位,可以使用以下代码:

using System;
using System.IO;

class Program
{
    static void Main(string[] args)
    {
        string filePath = 'path_to_file';
        long fileSizeInBytes = new FileInfo(filePath).Length;
        double fileSizeInMB = (double)fileSizeInBytes / (1024 * 1024);

        Console.WriteLine($'File size: {fileSizeInMB} MB');
    }
}

请将'path_to_file'替换为要读取大小的文件的路径。代码使用FileInfo类获取文件的大小(以字节为单位),然后将其转换为MB。

请注意,这里将1MB定义为1024 * 1024字节,而不是1000 * 1000字节,因为在计算机中,文件大小通常是以2的幂进行存储和计算的。

C# 获取文件大小(以MB为单位)

原文地址: https://www.cveoy.top/t/topic/pfYd 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录