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?
Directory.Delete
When you need to Remove folder
Directory. Create
When you need to Create folder
Directory.GetFiles
When you need to get list of files under specific path
Directory.GetDirectories
When you need to get list of folders under specific path
Directory.GetCreationTime
When you need to extract the folder creation time
Directory.GetParent
When you need to get the folder parent folder
Directory. Move
When you need to move folder for different location
Directory.GetLogicalDrives
When you need to get your server logical drives
Directory.Exist
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");

Note!
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);


Note!
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");

Note!
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)
            {
                Console.WriteLine(item);
            }


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)
            {
                Console.WriteLine(item);
            }
Note!
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)
            {
                Console.WriteLine(item);
            }

Note!
You can use search patter by using:

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


No comments:

Post a Comment

My Presentations