C# FileInfo - FileInfo Class in C# with Examples



C# FileInfo - FileInfo Class in C# with Examples

  • In C# File info class is used to deal with file and its operations and it uses Stream Writer class to write data to the file.
  • It provides methods and properties that are used to create, delete and read file and it is a part of system.IO namespace.

Syntax

[SerializableAttribute]  
[ComVisibleAttribute(true)]  
public sealed class FileInfo : FileSystemInfo 

File Info Constructors

Constructor Description
File Info (String) It is used to initialize a new instance of the File Info class which acts as a wrapper for a file path.

File Info Properties

Properties Description
Attributes It is used to get or set the attributes for the current file or directory.
Creation Time 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.
Directory Name 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.
Full Name It is used to get the full path of the directory or file.
Is Read Only It is used to get or set a value that determines if the current file is read only.
Last Access Time 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.

File Info Methods

Methods Description
Append Text () It is used to create a Stream Writer that appends text to the file represented by this instance of the File Info.
Copy to String () It is used to copy an existing file to a new file.
Create () It is used to create a file.
Create Text () It is used to create a Stream Writer 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.
Get Access Control () It is used to get a File Security object that encapsulates the access control list (ACL) entries.
Move To (String) It is used to move a specified file to a new specified location.
Open (File mode) It is used to open a file in the specified mode.
Open Read () It is used to create a read-only File Stream.
Open Text () It is used to create a Stream Reader with UTF8 encoding that reads from an existing text file.
Open Write () It is used to create a write-only File Stream.
Refresh () It is used to refresh the state of the object.
Replace (String) It is used to replace the contents of a specified file with the file described by the current File Info object.
To String () It is used to return the path as a string.

Sample Code

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 Successfuly

Related Searches to C# FileInfo - FileInfo Class in C# with Examples