Directory Ebook
Directory Ebook
Directory Ebook
This free book is provided by courtesy of C# Corner and its authors. Feel free to
share this book with your friends and co-workers. Please do not reproduce,
republish, edit or copy this book.
Mahesh Chand
©2012 C# Corner.
SHARE THIS DOCUMENT AS IT IS. DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Message from the Author
Thank you for being a part of C# Corner, a free online community for IT developers and
professionals.
I’ve always been a big believer and advocate of free knowledge sharing and education to all. C#
Corner is all about helping each other.
I will have to say a big thank you to you and our top members who made all this possible. This is
all possible because of you and you believing in sharing your code and knowledge.
Please feel free to share this book with your friends and co-workers and do not forget to share
your knowledge and spread the word around about C# Corner and the Mindcracker Network.
Namaste!
Mahesh Chand
Microsoft MVP, Visual C#
Founder, C# Corner and Mindcracker Network
©2012 C# Corner.
SHARE THIS DOCUMENT AS IT IS. DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Introduction
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.
©2012 C# Corner.
SHARE THIS DOCUMENT AS IT IS. DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Directory
The System.IO.Directory class in the .NET Framework class library provides static methods for
creating, copying, moving, and deleting directories and subdirectories. Before you can use the
Directory class, you must import the System.IO namespace.
using System.IO;
Create a Directory
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 compute.
The following code snippet creates a Temp folder in C:\ drive if the directory does not exists
already.
The Directory.CreateDirectory is also used to create a sub directory. All you have to do is to
specify the path of the directory in which this subdirectory will be created in. The following code
snippet creates a Mahesh subdirectory in C:\Temp directory.
Delete a directory in C#
©2012 C# Corner.
SHARE THIS DOCUMENT AS IT IS. DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
The Directory.Delete method deletes an empty directory from the specified path permanently. If
a directory has subdirectories 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 checks if a directory has subdirectories and files and delete them
before deleting a directory.
The Directory.Exists method checks if the specified directory exists. The following code snippet
checks if a directory exists or not and deletes only if the directory exists.
Move a directory in C#
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.
©2012 C# Corner.
SHARE THIS DOCUMENT AS IT IS. DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Copy a directory in C#
There is no method to copy a directory. The copying a directory is a process of creating a new
directory that you to want a directory to move to and then copying subdirectory and files.
The SetCreationTime and GetCreationTime methods are used to set and get the creation date
and time of the specified file. The following code snippet sets and gets the creation time of a
file.
The SetLastAccessTime and GetLastAccessTime methods are used to set and get the last access
date and time of the specified file. The following code snippet sets and gets the last access date
and time of a file.
The SetLastWriteTime and GetLastWriteTime methods are used to set and get the last write date
and time of the specified file. The following code snippet sets and gets the last write date and
time of a file.
©2012 C# Corner.
SHARE THIS DOCUMENT AS IT IS. DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Enumerate Directory in C#
Enumerate Files in C#
©2012 C# Corner.
SHARE THIS DOCUMENT AS IT IS. DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Get and Set File Creation Time
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.
The SetLastAccessTime and GetLastAccessTime methods are used to set and get the last access
date and time of the specified directory. The following code snippet sets and gets the last access
date and time of a directory.
The SetLastWriteTime and GetLastWriteTime methods are used to set and get the last write date
and time of the specified directory. The following code snippet sets and gets the last write date
and time of a directory.
The SetCurrentDirectory method sets the specified directory as the current directory. The
GetCurrentDirectory method returns the current directory.
©2012 C# Corner.
SHARE THIS DOCUMENT AS IT IS. DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Console.WriteLine(Directory.GetCurrentDirectory());
The GetDirectories method of the Directory class loads all the subdirectories of a directory. To
get all subdirectories, we can read subdirectories recursively.
The GetRootDirecoty method returns the root directory of the specified directory.
©2012 C# Corner.
SHARE THIS DOCUMENT AS IT IS. DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.
Help Us
There are many ways you can help us grow this free community.
Spread the word around by letting your co-workers and friends know
about C# Corner.
Share an article or book links with them.
By posting your articles, code snippets, tips and blogs online >>
By answering questions on C# Corner Answers >>
By sharing your Interview questions or posting interview answers >>
By sharing software and IT related news here >>
By simply visiting C# Corner and letting us know how we are doing and
what more we have to do to improve our services.
©2012 C# Corner.
SHARE THIS DOCUMENT AS IT IS. DO NOT REPRODUCE, REPUBLISH, CHANGE OR COPY.