Saturday, February 11, 2012

Working with File Class in .Net (C#)

File class is static class provides static methods to do normal file related operationsIt is available in System.IO namespaceFile class used to do some file related operations like Copy, Paste, Move, Replace, Delete etc. Let’s have a look on below code.

string filePath = @"D:\Temp\Test.txt";
//Checkes whether Test.txt file is exists or not
if (File.Exists(filePath))
    //Copies Test.txt file to TestCopy.txt.
    File.Copy(filePath, @"D:\Temp\TestCopy.txt", true);
    //Moves Test.txt file to TestMove.txt.
    //Throws an exception if file already exists.
    File.Move(filePath, @"D:\Temp\TestMove.txt");
    //Replace TextMove.txt file with TextCopy.txt file with
    //provisional backup file TestBKP.txt.
    File.Replace(@"D:\Temp\TestMove.txt", @"D:\Temp\TestCopy.txt", @"D:\Temp\TestBKP.txt");      

    //Deletes TextBKP.txt file.
    //Throws an exception if file is readonly.
    //Creates new file named Test.txt

Getting File Attributes

File class provides SetAttributes and GetAttributes methods to set attributes to file. The file attributes enum contains value like Archieve, Normal, Hidden, ReadOnly etc. We can change file attributes using Get/Set attributes method of File Class. Let’s have a look on below code.

//sets hidden attribute to Text.txt file.
File.SetAttributes(@"d:\temp\Test.txt", FileAttributes.Hidden);

//returns attributes of Test.txt
FileAttributes fileAttribute = File.GetAttributes(@"d:\temp\Test.txt");

if ((fileAttribute & FileAttributes.Hidden) == FileAttributes.Hidden)
    Console.WriteLine("This file is hidden");
    Console.WriteLine("this file is not hidden");

Getting Creation/Modification Times

File class provides methods to get creation time and modified time of file.

Method Name
Gets creation time of specified file.
Gets last modified date time of specified file
Gets last accessed date time of specified file.
Sets creation date and time to file.
Sets last write date and time to file.
Sets last accessed date and time to file.

Let’s have a look on below code.

//Gets creation time of specified file.
DateTime createdTime = File.GetCreationTime(@"d:\temp\Test.txt");

//Gets last modified date time of specified file.
DateTime modifiedTime = File.GetLastWriteTime(@"d:\temp\Test.txt");

//Gets last accessed date time of specified file.
DateTime lastAccessTime = File.GetAccessTime(@"d:\temp\Test.txt");

//Sets creation date and time to file.
File.SetCreationTime(@"d:\temp\Test.txt", DateTime.Now);

//Sets last accessed date and time to file.
File.SetLastAccessTime(@"d:\temp\Test.txt", DateTime.Now.AddMinutes(5));

//Sets last write date and time to file.
File.SetLastWriteTime(@"d:\temp\Test.txt", DateTime.Now.AddMinutes(10));

Output –
20-01-2012 08:09:37 PM
20-01-2012 08:19:37 PM
20-01-2012 08:14:37 PM

Encrypt / Decrypt file

Encrypt method of file class can encrypt file or directory while Decrypt method of file class can decrypt file. Using Encrypt and Decrypt we can protect file or folder from unwanted access.


When file encrypted the file name appears as green color in window 7. When file decrypted it will be appear as normal.

FileInfo Class

FileInfo class provides method to get file related information such as length, name, directory name, extension etc. Let’s have a look on below code.

FileInfo fileInfo = new FileInfo(@"d:\temp\test.txt");
Console.WriteLine("Name - {0}", fileInfo.Name);
Console.WriteLine("Full Name - {0}", fileInfo.FullName);
Console.WriteLine("Directory Name - {0}", fileInfo.DirectoryName);
Console.WriteLine("File Length - {0}", fileInfo.Length);
Console.WriteLine("File Extension - {0}", fileInfo.Extension);
Console.WriteLine("File Attributes - {0}", fileInfo.Attributes);
Console.WriteLine("Created On - {0}", fileInfo.CreationTime);
Console.WriteLine("Modified On - {0}", fileInfo.LastWriteTime);

Output -
Name - test.txt
Full Name - d:\temp\test.txt
Directory Name - d:\temp
File Length - 9
File Extension - .txt
File Attributes - Archive
Created On - 20-01-2012 08:14:48 PM
Modified On - 20-01-2012 08:18:04 PM

No comments:

Post a Comment