Creating new file and deleting file within application is one of the most common requirement in any enterprise system development, and there are various type of challenges like security, pre-exists file etc.
In this tutorial you will learn how to use C# FileInfo class for all file related operation. create and delete file in c# using fileinfo class.
In following example you will learn about Fileinfo class in C# .Net, How to create file, delete file using Fileinfo in C# .Net .
FileInfo class in .Net comes under System.IO namespace.
The FileInfo class provides the almost same functionality as the static System.IO.File class in .Net,
in FileInfo we have more to do on read/write operations on files by manually implementing code.
Here are some very useful properties and method of System.IO.FileInfo class, best way to understand following properties of FileInfo class, create a new instance of FileInfo
//Create a new instance of FileInfo for specified file
FileInfo fi = new FileInfo(@"D:\myfile.txt");
Now simply type fi., you will be able to see all properties and methods
Get the Directory name of the file (full path)
Get the DirectoryInfo object, there you can get all information about that Directory
c# fileinfo file exists example, this method will allow you to check if file exists.
string fileName = @"D:\application\myfile.txt";
FileInfo fi = new FileInfo(fileName);
if (fi.Exists)
Console.WriteLine(fi.FullName);
Before you work with any file, you can check if file exists
string fileName = @"D:\application\myfile.txt"; FileInfo fi = new FileInfo(fileName); Console.WriteLine(fi.Extension);
Get the file extension, like .txt etc.
string fileName = @"D:\application\myfile.txt"; FileInfo fi = new FileInfo(fileName); fi.Create();
Creates a new file
string fileName = @"D:\application\myfile.txt"; FileInfo fi = new FileInfo(fileName); fi.Delete();
Deletes the specified file permanently
Move any specified file to a new location, c# fileinfo move file
Encrypt the file so that other account user can't open the file
Decrypt the file that was encrypted by the same account it was Encrypted with
Gets a FileSecurity object of a specified file.
Replace the contents of a specified file, delete the original file, and create a backup of old file.
Create a StreamWriter object that appends text to the file instance of the FileInfo class.
Here we create a new file with some content in it, notice how to create byte array and how to write byte array using FileStream class.
// File name with full path
string fileName= @"D:\application\myfile.txt";
FileInfo fi = new FileInfo(fileName);
// Create a new file
using (FileStream fs = fi.Create())
{
Byte[] txt = new UTF8Encoding(true).GetBytes("We are adding some text in the file");
fs.Write(txt, 0, txt.Length);
Byte[] author = new UTF8Encoding(true).GetBytes("WebTrainingRoom.Com");
fs.Write(author, 0, author.Length);
}
Before checking any file information we need to check if file exists, (Also learn how to get file name, extension, DirectoryName) otherwise it will throw null object reference error
// File name with full path
string fileName= @"D:\application\myfile.txt";
FileInfo fi = new FileInfo(fileName);
// to check if file exists
if (fi.Exists)
{
// Get file name with full path
string fullFileName = fi.Name;
Console.WriteLine("File Name: {0}", fullFileName);
// Get file extension
string fileExtension = fi.Extension;
Console.WriteLine("File Extension: {0}", fileExtension);
// Get directory object
DirectoryInfo directory = fi.Directory;
Console.WriteLine("Directory: {0}", directory.Name);
// Get directory name
string directoryName = fi.DirectoryName;
Console.WriteLine("Directory Name: {0}", directoryName);
}
We also can set file permission using FileInfo class