Working with Directory in C#

Sourav.Kayal
Posted by in C# category on for Beginner level | Points: 250 | Views : 2715 red flag

In this article we will look into how to perform basic operation with directory in C#

Working with Directory in C#

In this article, we will see how to work with directory in C#.  .NET framework has a great namespace called System.IO which gives an option to work with I/O system like file and directory. Lets learn input and output  first.

Input and output (I/O) streaming is a process of reading data from and writing data to a storage medium. In the .NET Framework, the System.IO namespace and its sub namespaces contain the classes and other types to perform I/O operations. The Directory class in the .NET Framework class library provides static methods for creating,reading, copying, moving, and deleting directories. In this article we will see all those operation one by one.


Create a Directory 

Creating a directory is very easy. the Directory.CreateDirectory method creates a directory in the specified path with the specified Windows security. You can also create a directory on a remote computer.  The following code snippet creates a Temp folder in C:\ drive if the directory exists already.  

 

string root = @"C:\Sourav";
string subdir = @"C:\sourav\Kayal";
// If directory does not exist, create it.
if (!Directory.Exists(root))
{
    Directory.CreateDirectory(root);
}

Within if condition, we are checking whether directory already exists or  not using Directory.Exists() function.

Deleting a directory

The Directory.Delete method deletes an empty directory from the specified path permanently. If a directory has sub directories and/or files, you must delete them before you can delete a directory.  If you try to delete a file that is not empty, you will get an error message. The following code snippet deletes the destination file.  

string root = @"C:\Sourav";      
// If directory does not exist, don't even try
if (Directory.Exists(root))
{
    Directory.Delete(root);
}

The following code snippet checks if a directory has subdirectories and files and delete them before deleting a directory.  

Move a directory in C#

If we want we can move directory from one location to another location. The Directory.Move method moves an existing directory to a new specified directory with full path.  The Move method takes two parameters. The Move method deletes the original directory. 

The following code snippet moves the source directory to the destination directory.  

string sourceDirName = @"D:\Sourav";
string destDirName = @"D:\NewSourav";
try
{
    Directory.Move(sourceDirName, destDirName);
}
catch (IOException exp)
{
    Console.WriteLine(exp.Message);
}

Here my source directory is in D:Sourav and new directory is D:NewSourav. Display creation time of directory

Get and Set File Creation Time

It’s very easy to find directory information from any directory using c#. The Directory.SetCreationTime and Directory.GetCreationTime methods are used to set and get the creation date and time of the specified directory. The following code snippet sets and gets the creation time of a directory.

string root = @"C:\sourav";            
// Get and Set Creation time
Directory.SetCreationTime(root, DateTime.Now);
DateTime CreationTime = Directory.GetCreationTime(root);
Console.WriteLine(CreationTime);

In output it will show creation date time of directory.

Find all files from Directory

It’s rely easy to find all files from any directory what you think, the GetFiles() method gets a list of files in the specified directory.

string root = @"C:\Sourav";
string[] fileEntries = Directory.GetFiles(root);
foreach (string fileName in fileEntries)
Console.WriteLine(fileName);

In output it will show all file name within Sourav Directory.

Conclusion:

In this article we looked into how simple directory operation in C# can be performed. Hope this will help to beginner.

 


Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)