Click here to Skip to main content
15,894,460 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have some Folders in "F:\Source"
and i Want to move it to "F:\Destination"
After Moving the files i want the Source folder in the same Directory

What I have tried:

C#
System.IO.Directory.Move(@"F:\Source\", @"F:\Destination");


This is what i tried but after moving the files I cant find the Source folder in "F:\" Directory

Help me Please
Posted
Updated 6-Jul-17 1:23am

I googled "copy directory c#" and found this in about 10 seconds

How to: Copy Directories | Microsoft Docs[^]
 
Share this answer
 
Directory.Move[^] is supposed to do so. It moves the specified folder along with it's sub-folders and files to the specified location.
If you want just to copy and not move, you can use- System.IO.File.Copy on loop for each files or may be FileSystem.CopyDirectory[^] can help.
Or if you want just the folder contents to be moved, please check following if it helps-
directory - Move all files in subfolders to another folder using c# - Stack Overflow[^]

Thanks :)
 
Share this answer
 
v2
Here are some routines that I tried and tested:
C#
/// <summary>
        /// Recursively copy directory (XCOPY style), except file to skip.
        /// Existing files will be overwritten.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="target">The target.</param>
        /// <param name="skipFileName">File to skip.</param>
        public static void CopyAll(string source, string target, string skipFileName)
        {
            var sourceDi = new DirectoryInfo(source);
            var targetDi = new DirectoryInfo(target);
            CopyAll(sourceDi, targetDi, skipFileName);
        }

        /// <summary>
        /// Recursively copy directory (XCOPY style), except file to skip.
        /// Existing files will be overwritten.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="target">The target.</param>
        /// <param name="skipFileName">File to skip.</param>
        public static void CopyAll(DirectoryInfo source, DirectoryInfo target, string skipFileName)
        {
            // Check if the target directory exists, if not, create it.
            if (Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);
            }

            // Copy each file into it’s new directory, except files to skip.
            foreach (var fileInfo in source.GetFiles())
            {
                if (fileInfo.Name != skipFileName)
                {
                    Debug.Print(@"Copying {0}\{1}", target.FullName, fileInfo.Name);
                    fileInfo.CopyTo(Path.Combine(target.ToString(), fileInfo.Name), true);
                }
            }

            // Copy each subdirectory using recursion.
            foreach (var subDir in source.GetDirectories())
            {
                if (subDir.Name != target.Name)
                {
                    var nextTargetSubDir = target.CreateSubdirectory(subDir.Name);
                    CopyAll(subDir, nextTargetSubDir, skipFileName);
                }
            }
        }
 
Share this answer
 
string sourceDir = @"D:\Folder0";
        string destinationDir = @"D:\Folder1";

        try
        {
            // Ensure the source directory exists
            if (Directory.Exists(sourceDir) == true )
            {
                // Ensure the destination directory doesn't already exist
                if (Directory.Exists(destinationDir) == false)
                {
                    // Perform the move
                    Directory.Move(sourceDir, destinationDir);
                }
                else
                {
                    // Could provide the user the option to delete the existing directory
                    // before moving the source directory
                }
            }
            else
            {
                // Do something about the source directory not existing
            }
        }
        catch (Exception)
        {
            // TODO: Handle the exception that has been thrown
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900