The FileInfo class is employed for managing files and performing file-related tasks in C#. It offers a range of properties and functions to facilitate file creation, deletion, and reading. Utilizing the StreamWriter class, it enables the writing of data to files, and it is situated within the System.IO namespace.
C# FileInfo Class Signature
[SerializableAttribute]
[ComVisibleAttribute(true)]
public sealed class FileInfo : FileSystemInfo
C# FileInfo Constructors
The table below displays various constructors available for the FileInfo class.
| Constructor | Description |
|---|---|
| FileInfo(String) | It is used to initialize a new instance of the FileInfo class which acts as a wrapper for a file path. |
C# FileInfo Properties
The table below displays characteristics of the FileInfo class.
| Properties | Description |
|---|---|
| Attributes | It is used to get or set the attributes for the current file or directory. |
| CreationTime | It is used to get or set the creation time of the current file or directory. |
| Directory | It is used to get an instance of the parent directory. |
| DirectoryName | It is used to get a string representing the directory's full path. |
| Exists | It is used to get a value indicating whether a file exists. |
| FullName | It is used to get the full path of the directory or file. |
| IsReadOnly | It is used to get or set a value that determines if the current file is read only. |
| LastAccessTime | It is used to get or set the time from current file or directory was last accessed. |
| Length | It is used to get the size in bytes of the current file. |
Name |
It is used to get the name of the file. |
C# FileInfo Methods
The table below displays various techniques provided by the FileInfo class.
| Method | Description |
|---|---|
| AppendText() | It is used to create a StreamWriter that appends text to the file represented by this instance of the FileInfo. |
| CopyTo(String) | It is used to copy an existing file to a new file. |
| Create() | It is used to create a file. |
| CreateText() | It is used to create a StreamWriter that writes a new text file. |
| Decrypt() | It is used to decrypt a file that was encrypted by the current account using the Encrypt method. |
| Delete() | It is used to permanently delete a file. |
| Encrypt() | It is used to encrypt a file so that only the account used to encrypt the file can decrypt it. |
| GetAccessControl() | It is used to get a FileSecurity object that encapsulates the access control list (ACL) entries. |
| MoveTo(String) | It is used to move a specified file to a new specified location. |
| Open(FileMode) | It is used to open a file in the specified mode. |
| OpenRead() | It is used to create a read-only FileStream. |
| OpenText() | It is used to create a StreamReader with UTF8 encoding that reads from an existing text file. |
| OpenWrite() | It is used to create a write-only FileStream. |
| Refresh() | It is used to refresh the state of the object. |
| Replace(String,String) | It is used to replace the contents of a specified file with the file described by the current FileInfo object. |
| ToString() | It is used to return the path as a string. |
C# FileInfo Example: Creating a File
using System;
using System.IO;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
try
{
// Specifying file location
string loc = "F:\\abc.txt";
// Creating FileInfo instance
FileInfo file = new FileInfo(loc);
// Creating an empty file
file.Create();
Console.WriteLine("File is created Successfuly");
}catch(IOException e)
{
Console.WriteLine("Something went wrong: "+e);
}
}
}
}
Output:
File is created Successfully
We have visibility into the contents of the F drive where a file named abc.txt has been generated. An image capture is provided underneath.
The <style> element is essential for defining a custom CSS class. This allows for the creation of unique styles that can be applied to various HTML elements on a webpage.
C# FileInfo Example: writing to the file
using System;
using System.IO;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
try
{
// Specifying file location
string loc = "F:\\abc.txt";
// Creating FileInfo instance
FileInfo file = new FileInfo(loc);
// Creating an file instance to write
StreamWriter sw = file.CreateText();
// Writing to the file
sw.WriteLine("This text is written to the file by using StreamWriter class.");
sw.Close();
}catch(IOException e)
{
Console.WriteLine("Something went wrong: "+e);
}
}
}
}
Output:
The <style> element defines a CSS class for creating a styled placeholder diagram. The class includes properties such as background color, border radius, padding, margin, and text alignment. Within the diagram, there is an icon element with specified font size and margin, as well as a text element with a specific color and font size. This allows for the creation of visually appealing and consistent placeholder diagrams in web development projects.
C# FileInfo Example: Reading text from the file
using System;
using System.IO;
namespace CSharpProgram
{
class Program
{
static void Main(string[] args)
{
try
{
// Specifying file to read
string loc = "F:\\abc.txt";
// Creating FileInfo instance
FileInfo file = new FileInfo(loc);
// Opening file to read
StreamReader sr = file.OpenText();
string data = "";
while ((data = sr.ReadLine()) != null)
{
Console.WriteLine(data);
}
}
catch (IOException e)
{
Console.WriteLine("Something went wrong: " + e);
}
}
}
}
Output: