怎么把一个文件夹内的所有内容整个的复制到另一个文件夹中

时间:2021-07-24 17:07:31
在.aspx.cs文件中怎么实现把一个文件夹内的所有内容整个的复制到另一个文件夹中,或者是把一个文件夹复制到另一个文件夹中,谢谢!

8 个解决方案

#1


利用循环复制

#2


public void CopyDir(string path, string destPath) 

DirectoryInfo source = new DirectoryInfo(path); 
DirectoryInfo target = new DirectoryInfo(destPath); 
if(source.Exists) 

if(!target.Parent.Exists) 
throw new Exception(); 
if(!target.Exists) 
target.Create(); 
foreach(FileInfo file in source.GetFiles) 

file.CopyTo(...); 

foreach(DirectoryInfo dir in source.GetDirectories()) 

CopyDir(dir.FullName, Path.Combine(target.FullName,dir.Name)); 



#3


有人在论坛帖了一个方法,试试:

public int GetFileCount()
          {     
               int ret = 0;
               foreach(Directive directive in Directives)
               {
                    if (SetCancel)
                    {
                         if (AskBeforeAbort())
                         {
                              //cancel for real
                              BackupCompleteEvent(EventArgs.Empty);
                              return 0;
                         }
                    }
                    ret += CountFilesInDir(new DirectoryInfo(directive.SrcDir));
               }
               return ret;
          }

          public void StartBackup()
          {
               foreach (Directive directive in Directives)
               {
                    if (SetCancel)
                    {
                         if (AskBeforeAbort())
                         {
                              //cancel for real
                              BackupCompleteEvent(EventArgs.Empty);
                              return;
                         }
                    }
                    BackupDir(directive.SrcDir, directive.DestDir, directive.Overwrite);
               }
               BackupCompleteEvent(EventArgs.Empty);
          }

          public int CountFilesInDir(DirectoryInfo rootDirInfo)
          {
               int fileCount = 0;
               FileInfo[] fileInfos = rootDirInfo.GetFiles();
               foreach (FileInfo fi in fileInfos)
               {
                    fileCount ++;
               }
               DirectoryInfo[] subDirInfos = rootDirInfo.GetDirectories();
               foreach (DirectoryInfo subDirInfo in subDirInfos)
               {
                    fileCount = fileCount + CountFilesInDir(subDirInfo);
               }
               return fileCount;
          }
          public void BackupDir(string srcDir, string destDir, bool overwrite)
          {
               DirectoryInfo srcDirInfo = new DirectoryInfo(srcDir);

               if (!(Directory.Exists(destDir)))
               {
                    DirectoryInfo di = new DirectoryInfo(destDir);
                    di.Create();
               }

               FileInfo[] fileInfos = srcDirInfo.GetFiles();
               foreach (FileInfo fi in fileInfos)
               {
                    string targetPath = Path.Combine(destDir, fi.Name);
                    if (File.Exists(targetPath))
                    {
                         if (fi.LastWriteTimeUtc > new FileInfo(targetPath).LastWriteTimeUtc) // overwrite
                         {
                              BackupEventArgs e = new BackupEventArgs(OperationTypeEnum.Overwrite, fi.FullName, targetPath);
                              BackupProcessEvent(e);
                              try
                              {
                                   fi.CopyTo(targetPath, true);
                              }
                              catch (IOException)
                              {

                              }
                         }
                         else // skip
                         {
                              BackupEventArgs e = new BackupEventArgs(OperationTypeEnum.Skip, fi.FullName, targetPath);
                              BackupProcessEvent(e);
                         }
                    }
                    else // copy
                    {
                         BackupEventArgs e = new BackupEventArgs(OperationTypeEnum.Copy, fi.FullName, targetPath);
                         BackupProcessEvent(e);
                         try
                         {
                              fi.CopyTo(targetPath, true);
                         }
                         catch (IOException)
                         {
                              //do something here later...
                         }
                    }
               }
               DirectoryInfo[] dirInfos = srcDirInfo.GetDirectories();
               foreach (DirectoryInfo di in dirInfos)
               {
                    BackupDir(di.FullName, Path.Combine(destDir, di.Name), overwrite);
               }
          }

#4


到这个帖子里自己找,还有vb.net的:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21052023.html

#5


n种方法,
找到
System.io.Directory这个类和
System.io.File这个类
用他们的方法就ok了,
在一种就是用
System.Diagnostics.Process.start("copy d:\a d:\b");

#6


可以用DirectoryInfo的方法,通过递归调用GetFiles和GetDirectory来进行复制,不过比较麻烦^_^

#7


the easier way is to use Win32 API SHFileOperation to do the copying, see

http://www.dotnet247.com/247reference/msgs/45/225766.aspx

or

http://www.thescarms.com/vbasic/fileops.asp

#8


<form id="test" method="post" runat="server">
<asp:TextBox id="IDtb" style="Z-INDEX: 101; LEFT: 304px; POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox>
<INPUT id="UP_FILE2" style="Z-INDEX: 102; LEFT: 8px; WIDTH: 246px; POSITION: absolute; TOP: 8px; HEIGHT: 22px" type="file" size="21" name="UP_FILE2" RUNAT="server">
<asp:button id="upbutton" style="Z-INDEX: 103; LEFT: 296px; POSITION: absolute; TOP: 8px" runat="server" Text="上传"></asp:button
</form>

private void upbutton_Click(object sender, System.EventArgs e)
{
string tofolderPath= Server.MapPath("../mtebook/"+IDtb.Text+"/");//虚拟目录下有个mtebook文件夹
string fromfolderPath=UP_FILE2.Value.ToString().Substring(0,UP_FILE2.Value.ToString().LastIndexOf(@"\"));
DirectoryInfo source = new DirectoryInfo(fromfolderPath);
Directory.CreateDirectory(tofolderPath);
DirectoryInfo target = new DirectoryInfo(tofolderPath); 
if(source.Exists) 

FileInfo[] filesources = source.GetFiles();
foreach (FileInfo filesource in filesources)
{
filesource.CopyTo( tofolder+filesource.Name,true);
}
}
}
谢谢楼上仁兄的帮助,这段程序我能试过了好使!

#1


利用循环复制

#2


public void CopyDir(string path, string destPath) 

DirectoryInfo source = new DirectoryInfo(path); 
DirectoryInfo target = new DirectoryInfo(destPath); 
if(source.Exists) 

if(!target.Parent.Exists) 
throw new Exception(); 
if(!target.Exists) 
target.Create(); 
foreach(FileInfo file in source.GetFiles) 

file.CopyTo(...); 

foreach(DirectoryInfo dir in source.GetDirectories()) 

CopyDir(dir.FullName, Path.Combine(target.FullName,dir.Name)); 



#3


有人在论坛帖了一个方法,试试:

public int GetFileCount()
          {     
               int ret = 0;
               foreach(Directive directive in Directives)
               {
                    if (SetCancel)
                    {
                         if (AskBeforeAbort())
                         {
                              //cancel for real
                              BackupCompleteEvent(EventArgs.Empty);
                              return 0;
                         }
                    }
                    ret += CountFilesInDir(new DirectoryInfo(directive.SrcDir));
               }
               return ret;
          }

          public void StartBackup()
          {
               foreach (Directive directive in Directives)
               {
                    if (SetCancel)
                    {
                         if (AskBeforeAbort())
                         {
                              //cancel for real
                              BackupCompleteEvent(EventArgs.Empty);
                              return;
                         }
                    }
                    BackupDir(directive.SrcDir, directive.DestDir, directive.Overwrite);
               }
               BackupCompleteEvent(EventArgs.Empty);
          }

          public int CountFilesInDir(DirectoryInfo rootDirInfo)
          {
               int fileCount = 0;
               FileInfo[] fileInfos = rootDirInfo.GetFiles();
               foreach (FileInfo fi in fileInfos)
               {
                    fileCount ++;
               }
               DirectoryInfo[] subDirInfos = rootDirInfo.GetDirectories();
               foreach (DirectoryInfo subDirInfo in subDirInfos)
               {
                    fileCount = fileCount + CountFilesInDir(subDirInfo);
               }
               return fileCount;
          }
          public void BackupDir(string srcDir, string destDir, bool overwrite)
          {
               DirectoryInfo srcDirInfo = new DirectoryInfo(srcDir);

               if (!(Directory.Exists(destDir)))
               {
                    DirectoryInfo di = new DirectoryInfo(destDir);
                    di.Create();
               }

               FileInfo[] fileInfos = srcDirInfo.GetFiles();
               foreach (FileInfo fi in fileInfos)
               {
                    string targetPath = Path.Combine(destDir, fi.Name);
                    if (File.Exists(targetPath))
                    {
                         if (fi.LastWriteTimeUtc > new FileInfo(targetPath).LastWriteTimeUtc) // overwrite
                         {
                              BackupEventArgs e = new BackupEventArgs(OperationTypeEnum.Overwrite, fi.FullName, targetPath);
                              BackupProcessEvent(e);
                              try
                              {
                                   fi.CopyTo(targetPath, true);
                              }
                              catch (IOException)
                              {

                              }
                         }
                         else // skip
                         {
                              BackupEventArgs e = new BackupEventArgs(OperationTypeEnum.Skip, fi.FullName, targetPath);
                              BackupProcessEvent(e);
                         }
                    }
                    else // copy
                    {
                         BackupEventArgs e = new BackupEventArgs(OperationTypeEnum.Copy, fi.FullName, targetPath);
                         BackupProcessEvent(e);
                         try
                         {
                              fi.CopyTo(targetPath, true);
                         }
                         catch (IOException)
                         {
                              //do something here later...
                         }
                    }
               }
               DirectoryInfo[] dirInfos = srcDirInfo.GetDirectories();
               foreach (DirectoryInfo di in dirInfos)
               {
                    BackupDir(di.FullName, Path.Combine(destDir, di.Name), overwrite);
               }
          }

#4


到这个帖子里自己找,还有vb.net的:

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21052023.html

#5


n种方法,
找到
System.io.Directory这个类和
System.io.File这个类
用他们的方法就ok了,
在一种就是用
System.Diagnostics.Process.start("copy d:\a d:\b");

#6


可以用DirectoryInfo的方法,通过递归调用GetFiles和GetDirectory来进行复制,不过比较麻烦^_^

#7


the easier way is to use Win32 API SHFileOperation to do the copying, see

http://www.dotnet247.com/247reference/msgs/45/225766.aspx

or

http://www.thescarms.com/vbasic/fileops.asp

#8


<form id="test" method="post" runat="server">
<asp:TextBox id="IDtb" style="Z-INDEX: 101; LEFT: 304px; POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox>
<INPUT id="UP_FILE2" style="Z-INDEX: 102; LEFT: 8px; WIDTH: 246px; POSITION: absolute; TOP: 8px; HEIGHT: 22px" type="file" size="21" name="UP_FILE2" RUNAT="server">
<asp:button id="upbutton" style="Z-INDEX: 103; LEFT: 296px; POSITION: absolute; TOP: 8px" runat="server" Text="上传"></asp:button
</form>

private void upbutton_Click(object sender, System.EventArgs e)
{
string tofolderPath= Server.MapPath("../mtebook/"+IDtb.Text+"/");//虚拟目录下有个mtebook文件夹
string fromfolderPath=UP_FILE2.Value.ToString().Substring(0,UP_FILE2.Value.ToString().LastIndexOf(@"\"));
DirectoryInfo source = new DirectoryInfo(fromfolderPath);
Directory.CreateDirectory(tofolderPath);
DirectoryInfo target = new DirectoryInfo(tofolderPath); 
if(source.Exists) 

FileInfo[] filesources = source.GetFiles();
foreach (FileInfo filesource in filesources)
{
filesource.CopyTo( tofolder+filesource.Name,true);
}
}
}
谢谢楼上仁兄的帮助,这段程序我能试过了好使!