Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move a folder from one Drive to another in c# [closed]

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, and ABC12 folder should be completely move to destination folder along with subfolder.

I am getting the following error:

"Source and destination path must have identical roots. Move will not work across volumes."

Here is my code:

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/");
like image 761
Abhay Singh Avatar asked Oct 22 '25 17:10

Abhay Singh


2 Answers

You need this:

static public void CopyFolder(string sourceFolder, string 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 );
    }
}
like image 97
Syed Farjad Zia Zaidi Avatar answered Oct 25 '25 06:10

Syed Farjad Zia Zaidi


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);
like image 37
suhaim Avatar answered Oct 25 '25 07:10

suhaim