FileInfo class is used to handle files and their operations in C#.
It provides properties and methods for creating, deleting and reading files. it uses The StreamWriter class writes data to a file. It is part of the System.IO namespace.
The Directory property retrieves an object representing the file's parent directory.
The DirectoryName property retrieves the full path of the parent directory
The Exists property checks whether a file exists before operating on it.
The IsReadOnly property retrieves or sets a value that specifies whether the file can be read. Revise.
Length retrieves the size of the file.
Name retrieves the name of the file.
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.
The above is the detailed content of How to get file size in C#?. For more information, please follow other related articles on the PHP Chinese website!