Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
directory
communitycreator

How to move a file or directory in C#

Surabhi Sharma

The Directory class in the System.IO provides the Move() method with multiple overloads, which can be used to move a directory or file from its current location to a new location.

Syntax

public static void Move (string sourceDirName, string destDirName);

This method takes the source directory/file path and the destination directory/file path as input. It creates a new directory with the destination directory name and moves the source directory (or file) to the destination.

Things to note

This method throws an exception:

  • If the source path or destination path is null or an empty string, or if it contains white space or invalid characters.

  • If the source path is invalid.

  • If the destination path is in a different volume.

  • If the destination path already exists.

  • If the input source path and input destination path refer to the same directory or file.

  • If the source directory or its contents are being used by another process.

  • If the caller does not have the required permission.

  • If the specified source, destination path, or file name exceeds the system-defined maximum allowed length.

In the example below, we first create a source directory with a file inside it. We also delete the destination directory if it already exists.

Next, we call the Directory.Move() method with the source directory and destination directory paths.

We then use the Directory.Exists() method to validate if the destination directory is moved successfully and exists at destination path.

The program prints the following output and exits.

/source/ is moved successfully to /destination/
using System;
using System.IO;

namespace DirectoryMover
{
    class Program
    {    
        static void Main(string[] args)
        {
            string sourceDir = "/source/";
            string destDir = "/destination/";
            CreateDirectoryData(sourceDir, destDir);
            try
            {
                Directory.Move(sourceDir, destDir);
                if(Directory.Exists(destDir))
                {
                    Console.WriteLine("{0} is moved successfully to {1}", sourceDir, destDir);                   
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private static void CreateDirectoryData(string source, string destination)
        { 
          string filePath = source + "/newfile.txt";
          if(!Directory.Exists(source))
          {
              Directory.CreateDirectory(source);
              if(!File.Exists(filePath))
              {
                  File.WriteAllText(filePath, "sample text");         
              }              
          }

          if(Directory.Exists(destination))
          {
              Directory.Delete(destination, true);
          }
        }
    }
}
Move a directory with Directory.Move(string, string)

RELATED TAGS

c#
directory
communitycreator
RELATED COURSES

View all Courses

Keep Exploring