Reader Level:
ARTICLE

Move a directory in C#

On July 17, 2012 Articles | Files, Directory, IO
How to move a directory using C# and .NET.
  • 0
  • 0
  • 9287

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#


Mahesh Chand

Mahesh Chand is founder of C# Corner. C# Corner founded in 1999 is a FREE member contributions based open platform for developers to solve problems, learn new technology and hang out.  M...

  • 1rank
  • 71M readers
  • 9times
COMMENT USING

Trending up