ARTICLE

Move a directory in C#

Posted by Mahesh Chand Articles | Files, Directory, IO July 17, 2012
How to move a directory using C# and .NET.
Reader Level:

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;

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.  

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


Download free book: Working with Directories in C#


Login to add your contents and source code to this article
comments
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Get Career Advice from Experts
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.