FileInfo
FileInfo is like a file's ID card that tells you its name, size, and dates, and lets you copy or delete it. It describes one specific file.
In this page:
Syntax
FileInfo info = new FileInfo(path);
info.Name;
info.Length;
info.Exists;
info.LastWriteTime;
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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Reading
info.Lengthfor a file that does not exist, which throws aFileNotFoundException; checkinfo.Existsfirst. - Reusing a
FileInfoafter the file changed and reading stale values, since properties are cached until you callinfo.Refresh(). - Treating
Lengthas the number of characters, when it is the file size in bytes.
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: