Provides an instance of a file, so that you can create, delete, move, and open a file. This class is immutable. If the state of the file represented by this object changes, this object will not reflect that change.
public sealed class FileInfo : FileSystemInfo
The FileInfo
class provides instance members that you can use to create,
delete, move, and open files. It also provides members for reading metadata about a file,
such as its size, creation time, and last accessed time.
To get a FileInfo
object, you can use the constructor
or the DirectoryInfo.GetFiles
method.
When you use the constructor to create a FileInfo
object, the file does not need to exist. However, many of the methods and properties of
FileInfo
will throw an exception if the file does not exist.
Constructor | Description |
---|---|
Initializes a new instance of the FileInfo class for the
specified file path.
Parameters
|
Property | Type | Description |
---|---|---|
System.IO.FileAttributes |
Gets or sets the specified attributes of the file.
This property is inherited from
FileSystemInfo .
|
|
System.DateTime |
Gets the creation date and time of the file.
This property is inherited from
FileSystemInfo .
|
|
System.DateTime |
Gets the creation date and time, in Coordinated Universal Time (UTC), of the file.
This property is inherited from
FileSystemInfo .
|
|
System.IO.DirectoryInfo |
Gets a DirectoryInfo object that represents the directory
containing the file.
|
|
System.String |
Gets the name of the directory that contains the file.
This property is inherited from
FileSystemInfo .
|
|
System.String |
Gets the extension of the file.
This property is inherited from
FileSystemInfo .
|
|
System.Boolean |
Gets a value indicating whether the file exists.
This property is inherited from
FileSystemInfo .
|
|
System.String |
Gets the full path of the file.
This property is inherited from
FileSystemInfo .
|
|
System.Boolean | Gets a value indicating whether the current file is read-only. | |
System.DateTime |
Gets the date and time the current file was last accessed.
This property is inherited from
FileSystemInfo .
|
|
System.DateTime |
Gets the date and time, in Coordinated Universal Time (UTC), the current file was last accessed.
This property is inherited from
FileSystemInfo .
|
|
System.DateTime |
Gets the date and time when the current file was last written to.
This property is inherited from
FileSystemInfo .
|
|
System.DateTime |
Gets the date and time, in Coordinated Universal Time (UTC), when the current file was last written to.
This property is inherited from
FileSystemInfo .
|
|
System.String |
Gets the name of the file.
This property is inherited from
FileSystemInfo .
|
|
System.Int64 | Gets the size, in bytes, of the current file. |
Method | Description |
---|---|
Creates or overwrites a file in the specified path.
ReturnsSystem.IO.FileStreamA new FileStream object.
Exceptions
|
|
Creates a new file or overwrites an existing file with the specified path.
ReturnsSystem.IO.StreamWriterA new StreamWriter object.
|
|
Deletes this file.
Exceptions
|
|
Moves and renames a file.
Parameters
Exceptions
|
|
Opens a file in the specified mode.
Parameters
ReturnsSystem.IO.FileStreamA FileStream object opened in the specified mode.
|
|
Opens a file in the specified mode and with the specified access.
Parameters
ReturnsSystem.IO.FileStreamA FileStream object opened in the specified mode and with the specified access.
|
|
Opens an existing file for reading.
ReturnsSystem.IO.FileStreamA FileStream that can be used to read the file.
|
|
Opens an existing UTF-8 encoded text file for reading.
ReturnsSystem.IO.StreamReaderA StreamReader object.
|
|
Opens an existing file or creates a new file for writing.
ReturnsSystem.IO.FileStreamA FileStream opened for writing.
|
|
Refreshes the state of the object.
This method is inherited from
FileSystemInfo .
|
The following example demonstrates how to use the FileInfo
class to create,
write to, read from, and delete a file.
using System;
using System.IO;
public class Example
{
public static void Main()
{
string fileName = "MyFile.txt";
FileInfo fileInfo = new FileInfo(fileName);
// Create the file and write to it
using (StreamWriter sw = fileInfo.CreateText())
{
sw.WriteLine("This is the first line.");
sw.WriteLine("This is the second line.");
}
Console.WriteLine($"File '{fileName}' created and written to.");
// Check if the file exists and get its size
if (fileInfo.Exists)
{
Console.WriteLine($"File exists. Size: {fileInfo.Length} bytes.");
// Read from the file
Console.WriteLine("Reading from the file:");
using (StreamReader sr = fileInfo.OpenText())
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
}
}
// Delete the file
fileInfo.Delete();
Console.WriteLine($"File '{fileName}' deleted.");
}
else
{
Console.WriteLine($"File '{fileName}' not found.");
}
}
}