C# FileInfo
last modified July 5, 2023
In this article we use FileInfo to work with files in C#.
FileInfo
FileInfo
provides properties and instance methods for the creation,
copying, deletion, moving, and opening of files. It aids in the creation of
FileStream objects.
There is a similar class called File
in .NET. While
FileInfo
is an instance of a file representing the file,
File
is a utility class for working with any file.
sky borrow war ocean cup cloud water read book look
In several examples, we use this text file.
C# FileInfo simple example
We have a simple example with FileInfo
.
var fi = new FileInfo("words.txt"); Console.WriteLine(fi.Name); Console.WriteLine(fi.FullName); bool ro = fi.IsReadOnly; if (ro) { Console.WriteLine("readonly file"); } else { Console.WriteLine("not a readonly file"); }
We create a FileInfo
object, passing a text file to its
constructor. Then we print its three properties.
Console.WriteLine(fi.Name); Console.WriteLine(fi.FullName);
We print its name and fullname.
bool ro = fi.IsReadOnly; if (ro) { Console.WriteLine("readonly file"); } else { Console.WriteLine("not a readonly file"); }
We determine whether the file is readonly.
$ dotnet run words.txt /home/jano/Documents/prog/csharp/fileinfo/First/words.txt not a readonly file
C# FileInfo create text file
The CreateText
method creates a StreamWriter
that
writes a new text file.
var fi = new FileInfo("colours.txt"); using StreamWriter sw = fi.CreateText(); sw.WriteLine("red"); sw.WriteLine("yellow"); sw.WriteLine("white"); sw.WriteLine("brown");
We create a new text file colours.txt
. We write four lines into the
file.
using StreamWriter sw = fi.CreateText();
The StreamWriter
class implements the IDisposable
interface; therefore, we use the using
statement to close the
resources when we are done.
C# FileInfo read file
The OpenText
creates a StreamReader
with UTF8 encoding
that reads from an existing text file.
var fi = new FileInfo("words.txt"); using StreamReader sr = fi.OpenText(); string? s = string.Empty; while ((s = sr.ReadLine()) != null) { Console.WriteLine(s); }
In the example, we read a file line by line.
$ dotnet run sky borrow war ocean cup cloud water read book look
C# FileInfo delete file
The Delete
method deletes a file.
var fi = new FileInfo("/etc/hostname"); try { fi.Delete(); } catch (UnauthorizedAccessException e) { Console.WriteLine("failed to delete file"); Console.WriteLine(e.Message); }
In the example, we try to delete a system file. Since we do not have enough permissions, an exception is thrown.
$ dotnet run failed to delete file Access to the path '/etc/hostname' is denied.
C# FileInfo copy file
The CopyTo
copies an existing file to a new file, disallowing the
overwriting of an existing file.
var fi = new FileInfo("words.txt"); fi.CopyTo("words2.txt");
The example creates a copy of the words.txt
file.
C# FileInfo Directory
The Directory
gets an instance of the parent directory.
var fi = new FileInfo("/etc/hostname"); var di = fi.Directory; Console.WriteLine(di?.FullName); Console.WriteLine(di?.LastAccessTime); Console.WriteLine(di?.Root);
The example prints the file's parent directory fullname, last access time and root directory.
$ dotnet run /etc 6/30/2022 11:46:07 AM /
Source
FileInfo class - language reference
In this article we have used FileInfo
to work with files.
Author
List all C# tutorials.