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/");
答案 0 :(得分:5)
你需要这个:
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 );
}
}
答案 1 :(得分:1)
仅当源和目标位于同一驱动器中时,移动功能才起作用
您可以使用复制,然后使用删除
请参阅以下链接,将目录复制到另一个驱动器
Copy a directory to a different drive
然后使用
Directory.Delete(source_path);
答案 2 :(得分:0)
您必须使用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;
}
}
}
答案 3 :(得分:-1)
您必须手动执行此操作,因为正如您所提到的,在评论中,您无法将其复制到两个不同的卷中。这是一个有据可查的事实。
Stream fs = File.OpenRead(@"C:\tools\a.csv");
Stream target = File.OpenWrite(@"D:\mystuff\a.csv");
fs.CopyTo(target);
target.Close();
fs.Close();
注意:自{4.0}以来,CopyTo()
已添加到Stream
类型
完成后,您可以删除原始文件。
File.Delete(@"C:\tools\a.csv");
答案 4 :(得分:-2)
使用中有什么问题?
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");
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