Home  >  Article  >  Backend Development  >  C# FileInfo

C# FileInfo

WBOY
WBOYOriginal
2024-09-03 15:23:39266browse

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

Working of C# FileInfo class

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:

  • FileInfo(string): A new instance of the FileInfo class is initialized and it acts as a wrapper for the path of the file.

There are several properties of the FileInfo class. They are explained as follows:

  • Attributes: We can get or set the attributes for the current file or the current directory using Attributes property.
  • CreationTime: We can get or set the creation time for the current file or the current directory using Creation Time property.
  • Directory: We can get an instance of the parent directory using Directory property.
  • DirectoryName: We can get a string that represents the full path of the directory using Directory Name property.
  • Exists: We can get a value that indicates if a file exists or no using Exists property.
  • FullName: We can get the full path of the directory or the full path of the file using Full Name property.
  • IsReadOnly: We can get or set a value that can determine if the current file has read-only property using Is Read Only property.
  • LastAccessTime: We can get or set the time at which the current file or the current directory was last accessed by using the Last access time property.
  • Length: We can get the size of the current file in bytes using the length property.
  • Name: We can get the name of the file by using the name property.

There are several methods of FileInfo class. They are explained as follows:

  • AppendText(): A stream writer is created using this method AppendText(). The text is appended to the file which is represented by the instance of the FileInfo class by using this stream writer.
  • CopyTo(String): An existing file can be copied to a new file using this method CopyTo(String).
  • Create(): A file can be created using this method Create().
  • CreateText(): A stream writer is created using this method CreateText()  and this stream writer writes to a new text file.
  • Decrypt(): A file can be decrypted using this method decrypt() which was originally encrypted by using the encrypt method by the current account.
  • Delete(): A file can be deleted permanently using the Delete() method.
  • Encrypt(): A file can be encrypted using Encrypt() method and this file can be decrypted by using Decrypt() method provided the account used for encryption is the same account used for decryption also.
  • GetAccessControl(): A file security object is obtained using this method GetAccessControl()  and it encapsulates the entries of the Access Control List (ACL).
  • MoveTo(String): A specified file can be moved from one location to a newly specified location using MoveTo(String) method.
  • Open(File Mode): A file can be opened in a specified mode using the Open(File Mode) method.
  • OpenRead(): A file stream that can be read-only can be created using the OpenRead() method.
  • OpenText(): A stream reader can be created using this method OpenText() which can read from an existing file with UTF8 encoding.
  • OpenWrite(): A file stream that can be written only can be created using this method OpenWrite().
  • Refresh(): The state of the object can be refreshed using this method Refresh().
  • Replace(String, String): The contents of a specified file can be replaced by the contents of the other file which is described by the current object of the FileInfo class using this method Replace(String, String).
  • ToString(): The path is returned as a string using this method ToString().

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:

C# FileInfo

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.

Example of C# FileInfo

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:

C# FileInfo

Conclusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:C# StreamWriterNext article:C# StreamWriter