Move a directory to another location in C#
This post will discuss how to move a directory to another location in C#.
1. Using Directory.Move() method
You can move the entire directory using the Directory class from the System.IO namespace. The Directory.Move() method is often used to move a directory and its contents to the specified location. The following example shows the usage of the Directory.Move() method by moving the directory specified by srcDir to the directory specified by destDir.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; using System.IO; public class Program { public static void Main() { string srcDir = @"C:\source"; string destDir = @"C:\destination"; Directory.Move(srcDir, destDir); } } |
Note that either the source or destination directory can be relative or absolute. The Directory.Move() method throws IOException if the destination directory already exists, and DirectoryNotFoundException if the source or the destination path cannot be found. You can handle them as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
using System; using System.IO; public class Program { public static void Main() { string srcDir = @"C:\source"; string destDir = @"C:\destination"; try { if (Directory.Exists(srcDir) && !Directory.Exists(destDir)) { Directory.Move(srcDir, destDir); } else { Console.WriteLine("Either directory is invalid, or destination already exists."); } } catch (Exception ex) { // handle other exceptions } } } |
You may want to delete the destination folder if it already exists. The following example copies a directory’s contents to a new location, which can be an existing directory:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
using System; using System.IO; public class Program { public static void Main() { string srcDir = @"C:\source"; string destDir = @"C:\destination"; try { if (Directory.Exists(srcDir)) { if (Directory.Exists(destDir)) { Directory.Delete(destDir, true); } Directory.Move(srcDir, destDir); } else { Console.WriteLine("The directory does not exists."); } } catch (Exception ex) { // handle other exceptions } } } |
2. Using DirectoryInfo.MoveTo() method
Another option is to use the DirectoryInfo.MoveTo() method for moving a DirectoryInfo instance and its contents to a new path. The following example demonstrates moving a directory using the DirectoryInfo class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.IO; public class Program { public static void Main() { string srcDir = @"C:\source"; string destDir = @"C:\destination"; DirectoryInfo di = new DirectoryInfo(srcDir); di.MoveTo(destDir); } } |
To avoid getting any exceptions, you can ensure that the source path specified by srcDir exist and the destination path specified by destDir does not exist. If the destination directory exists, delete all files and subdirectories in it before moving.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; using System.IO; public class Program { public static void Main() { string srcDir = @"C:\source"; string destDir = @"C:\destination"; try { DirectoryInfo sourceDi = new DirectoryInfo(srcDir); if (Directory.Exists(srcDir)) { if (Directory.Exists(destDir)) { DirectoryInfo destDi = new DirectoryInfo(destDir); destDi.Delete(true); } sourceDi.MoveTo(destDir); } else { Console.WriteLine("The directory does not exists."); } } catch (Exception ex) { // handle other exceptions } } } |
That’s all about moving a directory to another location in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)