Home >Backend Development >C#.Net Tutorial >C# FileInfo
To work with files in .NET framework, the important namespace used is system.IO namespace and similarly, we have a class called FileInfo class in C# which do not consist of static methods and only instantiated objects can use this class, a file on a disk or a location of a network is represented by the fileinfo object, filestream objects can be created with the help of fileinfo objects and instance methods are provided by file info class to create, delete, copy, move and open the files and we can have more control on the operations of reading and write on files to manually write the code which can be read or bytes can be written from a file using fileinfo class.
The syntax of C# FileInfo class is as follows:
[SerializableAttribute] [ComVisibleAttribute(true)] public sealed class FileInfo : FileSystemInfo
To understand the working of FileInfo class in C#, we need to understand the constructors of FileInfo class, properties of FileInfo class, and methods of FileInfo class.
The constructors of the FileInfo class are explained as below:
There are several properties of the FileInfo class. They are explained as follows:
There are several methods of FileInfo class. They are explained as follows:
As we have understood the constructors of FileInfo class, Properties of FileInfo class and methods of the FileInfo class, now consider the below program:
Code:
using System; using System.IO; namespace Program { class Check { static void Main(string[] args) { try { // the file location is specified where the file is to be created string location = "C:\Users\shivakumarsh\Desktop\new.txt"; // instance of the fileinfo class is created FileInfo file = new FileInfo(location); // an empty file is created file.Create(); Console.WriteLine("Creation of file is successfull"); } catch(IOException e) { Console.WriteLine("Failed attempt to create file "+e); } } } }
Output:
In the above program, a namespace called the program is declared. Then the main method consisting of the try-catch block is defined. The try block consists of the location string where the new file needs to be created. An instance of the file info class is created, and the location string is passed as a parameter to the instance of the file info class. Create () method is invoked on the object of the file info class to create a new file in the location specified by the location string. If the file creation is successful, the success message is printed otherwise an exception is raised which is included in the catch block.
C# program to demonstrate usage of File Info class.
Code:
using System; using System.IO; namespace Program { class Check { static void Main(string[] args) { // the file location is specified where the file is to be located string location = "C:\Users\shivakumarsh\Desktop\new.txt"; // instance of the fileinfo class is created FileInfo file = new FileInfo(location); // The specified file is deleted file.Delete(); Console.WriteLine("Deletion of file is successfull"); } } }
Output:
In this tutorial, we understand the concept of FileInfo class in C# through definition, constructors of FileInfo class, properties of FileInfo class, methods of FileInfo class, working of FileInfo class through examples.
The above is the detailed content of C# FileInfo. For more information, please follow other related articles on the PHP Chinese website!