Monday, August 26, 2013

C# - Working with File System Objects (Part 1)

This article will be the first one from a series of articales that will cover System I\O operations you can covered using C# classes.

Classes for I/O operations
C# contain multiple classes that we can used to handle I/O operations (Files /Folders) for this article you need to add the following 'using' in your code:

using System.Text;
using System.Threading.Tasks;
using System.IO;

By adding this namespace we now have access to different classes that can help us perform multiple operations on files and folders.

Main classes in System.Io namespace:

Class Directory

This class contain static methods for Directories manipulation, using this class you can do multiple operations for folders without using/Creating instances from class.

Class Summary:
This table contain the main methods you can use while working with this class.

Method Name
Why to use?
When you need to Remove folder
Directory. Create
When you need to Create folder
When you need to get list of files under specific path
When you need to get list of folders under specific path
When you need to extract the folder creation time
When you need to get the folder parent folder
Directory. Move
When you need to move folder for different location
When you need to get your server logical drives
When you need to check if folder existing

Code Examples:

Create Folder

This example create root folder under root drive with inner folder.
Directory.CreateDirectory("C:\\New Folder\\SubFolder");             

Add Folder for existing Tree

This code will add folder into the root folder we created on previous step:
Directory.CreateDirectory("C:\\New Folder\\SubFolder1");

Delete Folder

Example 1:
This code will delete an ‘Empty’ folder (No files or folder under given path)
Directory.Delete("C:\\New Folder\\SubFolder1");

Id objects resides under this path the program will crush with “IOException” error.

Example 2:
This code will delete root folder + sub objects (Folder /Files)

Directory.Delete("C:\\New Folder",true);

If folder opened or used by another process the software failed with “IOException error”

Check for folder existence under specific folder

This example send query to validate if specific folders located under a Directory path

Directory.Exists("C:\\New Folder\\SubFolder");           

Extract the Root folder for specific path

This code will return the folder that host the folder supplied in method
string  RootFolder = Directory.GetDirectoryRoot("c:\\1");

You can also extract the project current directory when using the “Directory.GetCurrentDirectory();” method.

Extracting dates for specific operations

This part will show how to receive dates for specific operations performed on a given folder

Creation Time:
DateTime FolderCreation2 = Directory.GetCreationTime("c:\\1");

Last modification time:
DateTime LastModificationTime = Directory.GetLastWriteTime("c:\\1");

Extracting the computer logical drives

This code will extract all logical drives located on computer, pay attention that we need to use an array to host the returned values.

string [] LogicalDrives = Directory.GetLogicalDrives();

            foreach (var item in LogicalDrives)

Extract folder list under specific path

This code will demonstrate how to extract the folder tree under specific path

string [] ArrayOfDirectories = Directory.GetDirectories("c:\\");

            foreach (var item in ArrayOfDirectories)
You can use search patter by using:

string[] SearchByPattern = Directory.GetDirectories("c:\\1", "FolderNmae");

Extract files list under specific path

This code will demonstrate how to extract the files tree under specific path

string [] ArrayOfFiles = Directory.GetFiles("c:\\1");

            foreach (var item in ArrayOfFiles)

You can use search patter by using:

string[] ArrayOfFiles = Directory.GetFiles("c:\\1", "Filename");

No comments:

Post a Comment

My Presentations