FileInfo类用于在C#中处理文件及其操作。
它提供了用于创建、删除和读取文件的属性和方法。它使用 StreamWriter 类将数据写入文件。它是 System.IO 命名空间的一部分。
Directory 属性检索表示文件父目录的对象。
DirectoryName 属性检索父目录的完整路径
Exists 属性在操作文件之前检查文件是否存在。
IsReadOnly 属性检索或设置一个值,该值指定文件是否可以被读取。 修改。
Length检索文件的大小。
Name检索文件的名称。
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.
以上就是C#中如何获取文件大小?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号