如何在C#中获取文件大小?
FileInfo类用于处理文件及其在C#中的操作。
它提供了用于创建,删除和读取文件的属性和方法。它使用StreamWriter类将数据写入文件。它是System.IO命名空间的一部分。
Directory属性检索代表文件父目录的对象。
DirectoryName属性检索文件的父目录的完整路径。
Exists属性在对其进行操作之前检查文件是否存在。
IsReadOnly属性检索或设置一个值,该值指定是否可以修改文件。
长度检索文件的大小。
名称检索文件的名称。
示例
class Program{
public static void Main(){
var path = @"C:\Users\Koushik\Desktop\Questions\ConsoleApp\Data.csv";
long length = new System.IO.FileInfo(path).Length;
System.Console.WriteLine(length);
}
}输出结果
12
示例
class Program{
public static void Main(){
var path = @"C:\Users\Koushik\Desktop\Questions\ConsoleApp";
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fiArr = di.GetFiles();
Console.WriteLine("The directory {0} contains the following files:", di.Name);
foreach (FileInfo f in fiArr)
Console.WriteLine("The size of {0} is {1} bytes.", f.Name, f.Length);
}
}输出结果
The directory ConsoleApp contains the following files: The size of ConsoleApp.csproj is 333 bytes. The size of Data.csv is 12 bytes. The size of Program.cs is 788 bytes.
热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短