繁体   English   中英

在c#中将文件夹从一个驱动器移动到另一个驱动器[关闭]

[英]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

我想将文件夹ABC12从E驱动器移动到GetContent文件夹中的目标路径,但是ABC12包含不同的子文件夹。 ABC12文件夹应与子文件夹一起完全移动到目标文件夹。 请帮我。

我收到以下错误:我收到这样的错误“源和目标路径必须具有相同的根。移动将无法跨卷工作。”

 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/");

你需要这个:

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 );
    }
}

仅当源和目标位于同一驱动器中时,移动功能才起作用

在这种情况下,您可以使用复制,然后使用删除

请参阅以下链接,将目录复制到另一个驱动器

将目录复制到其他驱动器

然后用

Directory.Delete(source_path);

您必须使用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;
    }
}

}

您必须手动执行此操作,因为正如您所提到的,在注释中,您无法将其复制到两个不同的卷中。 这是一个有据可查的事实。

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

注意:自.NET 4.0以来, CopyTo()被添加到Stream类型中

完成后,您可以删除原始文件。

File.Delete(@"C:\tools\a.csv");

使用中有什么问题?

Directory.Move(sourceDirectory, destinationDirectory);

要么

DirectoryInfo di = new DirectoryInfo("TempDir");

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

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


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

参考: 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM