3
Here is my source file and destination file";

Source :  E:\\Test\Test_Content\\ABC12
Destination: F:\\Test\GetContent

I want to move folder ABC12 from E drive to destination path within GetContent folder, However ABC12 contains different sub folder. ABC12 folder should be completely move to destination folder along with subfolder. Please help me.

I am getting following error: I am getting error like this "Source and destination path must have identical roots. Move will not work across volumes."

 string sfolder="Path of the folder to move which is in project directory in E drive";
 string path = "~/UContent" + "/" + sfolder;
                string extractfiles = Server.MapPath("UContent"+"/");
               System.IO.Directory.Move(extractfiles+"/"+sfolder,@"F:/GetContent/");
Abhay Singh
  • 1,595
  • 1
  • 23
  • 40
  • 1
    http://msdn.microsoft.com/en-us/library/bb762914(v=vs.110).aspx – Ashok Damani May 29 '14 at 07:11
  • 1
    Do you have a problem you need help with? One obvious option is to delete subfolders inside ABC12 first, so you no longer need to recursively copy content... Also how your question is related to `razor`?? – Alexei Levenkov May 29 '14 at 07:12
  • I am getting error like this "Source and destination path must have identical roots. Move will not work across volumes." – Abhay Singh May 29 '14 at 07:26
  • Your updated code is still ignoring the fact that you cannot use `Directory.Move` to another volume (in this case, this means "another drive"). You have to do a copy and then a deletion of the original folder. – Stephen Byrne May 29 '14 at 07:46
  • I have to require these things. So, will you please suggest me that how can I do this? – Abhay Singh May 29 '14 at 07:58

5 Answers5

5

You need this:

static public void CopyFolder(string sourceFolder, string destFolder )
{
    if (!Directory.Exists( destFolder ))
        Directory.CreateDirectory( destFolder );
    string[] files = Directory.GetFiles( sourceFolder );
    foreach (string file in files)
    {
        string name = Path.GetFileName( file );
        string dest = Path.Combine( destFolder, name );
        File.Copy( file, dest );
    }
    string[] folders = Directory.GetDirectories( sourceFolder );
    foreach (string folder in folders)
    {
       string name = Path.GetFileName( folder );
       string dest = Path.Combine( destFolder, name );
        CopyFolder( folder, dest );
    }
}
Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50
  • I am getting error like this "Source and destination path must have identical roots. Move will not work across volumes." – Abhay Singh May 29 '14 at 07:27
  • I have updated my code. Please have a look – Abhay Singh May 29 '14 at 07:31
  • It works fine ! thank you ! but in my case i was trying to move folder in the same drive and i was getting "Source and destination path must have identical roots. Move will not work across volumes." Error. why ?? – ghazyy Jul 15 '14 at 07:21
  • `Directory.CreateDirectory` is idempotent so there's no need to check if the directory exists before creating it. – devklick Dec 23 '20 at 13:31
1

Move function worked only if the source and destination is in the same drive

You can use Copy followed by a Remove in that case

Refer the below link for copying a directory to another drive

Copy a directory to a different drive

Then use

Directory.Delete(source_path);
Community
  • 1
  • 1
suhaim
  • 314
  • 2
  • 7
0

You have to use FileCopy

public class CopyFiles {

internal event EventHandler Successful;

internal event EventHandler Failed;

private string Reason;

private string mstrSource;

private string mstrDestination;

internal void StartCopying() {
    try {
        CopyDirectory(mstrSource, mstrDestination);
        Successful();
    }
    catch (Exception ex) {
        Failed(ex.Message);
    }
}

private bool CopyDirectory(string Src, string Dest) {
    // add Directory Seperator Character (\) for the string concatenation shown later
    if ((Dest.Substring((Dest.Length - 1), 1) != Path.DirectorySeparatorChar)) {
        Dest = (Dest + Path.DirectorySeparatorChar);
    }
    // If Directory.Exists(Dest) = False Then Directory.CreateDirectory(Dest)
    string[] Files = Directory.GetFileSystemEntries(Src);
    foreach (string element in Files) {
        if ((Directory.Exists(element) == true)) {
            // if the current FileSystemEntry is a directory,
            // call this function recursively
            Directory.CreateDirectory((Dest + Path.GetFileName(element)));
            CopyDirectory(element, (Dest + Path.GetFileName(element)));
        }
        else {
            // the current FileSystemEntry is a file so just copy it
            File.Copy(element, (Dest + Path.GetFileName(element)), true);
        }
    }
}

internal string Source {
    get {
        return mstrSource;
    }
    set {
        mstrSource = value;
    }
}

internal string Destination {
    get {
        return mstrDestination;
    }
    set {
        mstrDestination = value;
    }
}

}

-1

You'll have to do it manually, since, as you mentioned, in the comments, you cannot copy it across two different volumes. This is a well documented fact.

Stream fs = File.OpenRead(@"C:\tools\a.csv");
Stream target = File.OpenWrite(@"D:\mystuff\a.csv");
fs.CopyTo(target);
target.Close();
fs.Close();

Note: CopyTo() is added to Stream types since .NET 4.0

And once done, you can delete the original file.

File.Delete(@"C:\tools\a.csv");
deostroll
  • 11,661
  • 21
  • 90
  • 161
-2

What Problem in using?

Directory.Move(sourceDirectory, destinationDirectory);

Or

DirectoryInfo di = new DirectoryInfo("TempDir");

if (di.Exists == false)
    di.Create();

DirectoryInfo dis = di.CreateSubdirectory("SubDir");


if (Directory.Exists("NewTempDir") == false)
    di.MoveTo("NewTempDir");

ref: http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.moveto(v=vs.110).aspx http://msdn.microsoft.com/en-us/library/system.io.directory.move(v=vs.110).aspx

Nipun Ambastha
  • 2,553
  • 1
  • 16
  • 26