C Sharp FileInfo Class

C# FileInfo Class

The FileInfo class in C# facilitates properties and methods to deal with the file and its operations, i.e., to create, delete and read a file. Being a part of the System.IO namespace, it writes data to the file using the StreamWriter class.

C# FileInfo Class Signature:

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

C# FileInfo Constructors:

ConstructorUses
FileInfo(String)To initialize a new instance of the FileInfo class to serve as a wrapper for a file path.

C# FileInfo Properties:

PropertiesUses
AttributesTo retrieve or to set the attributes for the current file or directory.
CreationTimeTo retrieve or to set the creation time of the current file or directory.
DirectoryTo retrieve an instance of the parent directory.
DirectoryNameTo retrieve a string representing the directory’s full path.
ExistsTo retrieve a value indicating whether a file exists.
FullNameTo retrieve the full path of the directory or file.
IsReadOnlyTo retrieve or to set a value that determines if the current file is read only.
LastAccessTimeTo retrieve or to set the time from current file or directory when it was last accessed.
LengthTo retrieve the size in bytes of the current file.
NameTo retrieve the name of the file.

C# FileInfo Methods:

MethodUses
AppendText()For creating a StreamWriter to appends text to the file represented by this instance of the FileInfo.
CopyTo(String)To copy an existing file to a new file.
Create()To create a file.
CreateText()For creating a StreamWriter to write a new text file.
Decrypt()To decrypt a file encrypted by the current account using the Encrypt method.
Delete()To delete a file, permanently.
Encrypt()To encrypt a file. The file can be decrypted only by the account used to encrypt the file.
GetAccessControl()To return a FileSecurity object to encapsulate the access control list (ACL) entries.
MoveTo(String)To relocate the specified file to a new specified location.
Open(FileMode)To open a file in the specified mode.
OpenRead()For creating a read-only FileStream.
OpenText()For creating a StreamReader with UTF8 encoding to read from an existing text file.
OpenWrite()For creating a write-only FileStream.
Refresh()To refresh the state of the object.
Replace(String,String)To replace the contents of a specified file with the file described by the current FileInfo object.
ToString()To retrieve the path as a string.

Example:

using System;  
using System.IO;  
namespace Example  
{  
    class Content  
    {  
        static void Main(string[] args)  
        {  
            try  
            {  
                // Specifying file location  
                string address = "D:\\file.txt";  
                // Creating FileInfo instance  
                FileInfo info = new FileInfo(address);  
                // Creating an empty file  
                info.Create();  
                Console.WriteLine("Success!!");  
            }catch(IOException e)  
            {  
                Console.WriteLine("Failure: " + e);  
            }  
        }  
    }  
}

Output:

Explanation:

In the above example, we are using the C# FileInfo class for creating a file. Thus a file named file.txt will be created inside the D drive after the execution of the above program.

Example:

using System;  
using System.IO;  
namespace Example  
{  
    class Content  
    {  
        static void Main(string[] args)  
        {  
            try  
            {  
                // Specifying file location  
                string address = "D:\\file.txt";  
                // Creating FileInfo instance  
                FileInfo info = new FileInfo(address); 
                // Creating an file instance to write  
                StreamWriter writer = info.CreateText();  
                // Writing to the file  
                writer.WriteLine("Hello World!!");  
                writer.Close();  
            }catch(IOException e)  
            {  
                Console.WriteLine("Failure in execution: " + e);  
            }  
        }  
    }  
}

Output: D:\\file.txt:

Explanation:

In the above example, we are using the C# FileInfo class for writing text to a file.

Example:

using System;  
using System.IO;  
namespace CSharpProgram  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            try  
            {  
                // Specifying file to read  
                  string address = "D:\\file.txt";  
                // Creating FileInfo instance  
                FileInfo info = new FileInfo(address); 
                // Opening file to read  
                StreamReader reader = info.OpenText();  
                string str = "";  
                while ((str = reader.ReadLine()) != null)  
                {  
                     Console.WriteLine(str);  
                }  
            }  
            catch (IOException e)  
            {  
                Console.WriteLine("Failure in execution: " + e);  
            }  
        }  
    }  
}

Output:

Explanation:

In the above example, we are using the C# FileInfo class for reading text from a file.

Content Protection by DMCA.com
Please Share