FileInfo
In this page:
FileInfo
FileInfo represents a specific file and exposes instance properties like Length, Exists, and LastWriteTime, plus methods like Delete() and CopyTo(). Unlike the static File class, FileInfo caches some information after construction, which can be more efficient for repeated checks on the same file. It's the object-oriented counterpart to File's static methods.
Note: Use FileInfo when you need to inspect several properties of the same file — it avoids repeated filesystem calls that File's static methods make each time.
Example: FileInfo
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
string path = "info.txt";
File.WriteAllText(path, "some sample content");
FileInfo info = new FileInfo(path);
Console.WriteLine(info.Name);
Console.WriteLine(info.Length);
Console.WriteLine(info.Exists);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: