怎么把一個文件夾內的所有內容整個的復制到另一個文件夾中


在.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);
}
}
}
謝謝樓上仁兄的幫助,這段程序我能試過了好使!

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
  © 2014-2022 ITdaan.com 联系我们: