2013-10-04 18 views
-1

我有IIS部署的网站上我有虚拟文件夹包含文件夹和文件。 我正在使用以下代码从Http站点复制文件。但我一次只复制一个文件。而不是一个接一个地处理文件我想复制所有目录如何从Http目录复制所有文件?

private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs) 
    { 
     // Get the subdirectories for the specified directory. 
     DirectoryInfo dir = new DirectoryInfo(sourceDirName); 
     DirectoryInfo[] dirs = dir.GetDirectories(); 

     if (!dir.Exists) 
     { 
      throw new DirectoryNotFoundException(
       "Source directory does not exist or could not be found: " 
       + sourceDirName); 
     } 

     // If the destination directory doesn't exist, create it. 
     if (!Directory.Exists(destDirName)) 
     { 
      Directory.CreateDirectory(destDirName); 
     } 

     // Get the files in the directory and copy them to the new location. 
     FileInfo[] files = dir.GetFiles(); 
     foreach (FileInfo file in files) 
     { 
      string temppath = Path.Combine(destDirName, file.Name); 
      file.CopyTo(temppath, false); 
     } 

     // If copying subdirectories, copy them and their contents to new location. 
     if (copySubDirs) 
     { 
      foreach (DirectoryInfo subdir in dirs) 
      { 
       string temppath = Path.Combine(destDirName, subdir.Name); 
       DirectoryCopy(subdir.FullName, temppath, copySubDirs); 
      } 
     } 
    } 
+1

可能会帮助您http://stackoverflow.com/questions/124492/c-sharp-httpwebrequest-command-to-get-directory-listing – kaushik0033

+1

请参阅链接:http://www.codeproject.com/Articles/34415 /正在下载 - 通过HTTP连接的多文件 – kaushik0033

+0

@ kaushik0033:我明白了。老兄谢谢!该目录中的 –

回答

0

那么,你可以尝试让整个操作是异步的,但我不知道结果会是你满意的。我从来没有听说过可以一次复制所有内容的功能。在每个操作系统中都有一个等待写入的文件队列;)

如果操作花费太多时间,只需使用ajax并通知用户当前的进度,这样网站就不会对他没有任何影响任何通知。

+0

文件始终在更改。我怎样才能得到这些文件名?这就是为什么我想复制所有文件夹 –

0

如果您想使用FTP目录下载特定目录下的所有文件。

为了能够将所有文件从FTP目录下载到本地文件夹,您必须列出远程目录中的所有文件,然后逐个下载它们。您可以使用下面的代码来执行相同的操作:

string[] files = GetFileList(); 
    foreach (string file in files) 
    { 
     Download(file); 
    } 

    public string[] GetFileList() 
    { 
     string[] downloadFiles; 
     StringBuilder result = new StringBuilder(); 
     WebResponse response = null; 
     StreamReader reader = null; 
     try 
     { 
      FtpWebRequest reqFTP; 
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/")); 
      reqFTP.UseBinary = true; 
      reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); 
      reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; 
      reqFTP.Proxy = null; 
      reqFTP.KeepAlive = false; 
      reqFTP.UsePassive = false; 
      response = reqFTP.GetResponse(); 
      reader = new StreamReader(response.GetResponseStream()); 
      string line = reader.ReadLine(); 
      while (line != null) 
      { 
       result.Append(line); 
       result.Append("\n"); 
       line = reader.ReadLine(); 
      } 
      // to remove the trailing '\n' 
      result.Remove(result.ToString().LastIndexOf('\n'), 1); 
      return result.ToString().Split('\n'); 
     } 
     catch (Exception ex) 
     { 
      if (reader != null) 
      { 
       reader.Close(); 
      } 
      if (response != null) 
      { 
       response.Close(); 
      }     
      downloadFiles = null; 
      return downloadFiles; 
     } 
    } 

    private void Download(string file) 
    {      
     try 
     {     
      string uri = "ftp://" + ftpServerIP + "/" + remoteDir + "/" + file; 
      Uri serverUri = new Uri(uri); 
      if (serverUri.Scheme != Uri.UriSchemeFtp) 
      { 
       return; 
      }  
      FtpWebRequest reqFTP;     
      reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + remoteDir + "/" + file));         
      reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);     
      reqFTP.KeepAlive = false;     
      reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;         
      reqFTP.UseBinary = true; 
      reqFTP.Proxy = null;     
      reqFTP.UsePassive = false; 
      FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); 
      Stream responseStream = response.GetResponseStream(); 
      FileStream writeStream = new FileStream(localDestnDir + "\" + file, FileMode.Create);     
      int Length = 2048; 
      Byte[] buffer = new Byte[Length]; 
      int bytesRead = responseStream.Read(buffer, 0, Length);    
      while (bytesRead > 0) 
      { 
       writeStream.Write(buffer, 0, bytesRead); 
       bytesRead = responseStream.Read(buffer, 0, Length); 
      }     
      writeStream.Close(); 
      response.Close(); 
     } 
     catch (WebException wEx) 
     { 
      MessageBox.Show(wEx.Message, "Download Error"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "Download Error"); 
     } 
    } 
+0

我没有保存文件列表的地方。这些文件是由我的程序生成的,其名称和计数总是在变化 –

0

复制目录不存在。您创建一个新的目标目录并复制源目录中的所有文件。如果源目录包含目录,则为其中的每个目录重复该过程,并且无限期地进行。

您的comment这里表示您确实试图解决另一个问题。那是什么问题?

如果您的实际问题是,文件消失,file.CopyTo()之间dir.GetFiles(),应用适当的try..catch条款赶不存在了文件的错误。

如果您的实际问题是,文件是dir.GetFiles()file.CopyTo()之间增加,不断档,你没有复制,再打电话dir.GetFiles()复制所有后相交结果的名称的列表,以查看是否有新的文件添加。

相关问题