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 )
{
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);
你必须使用文件复制
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