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#中如何获取文件大小?的详细内容。