2013-10-04 25 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()複製所有後相交結果的名稱的列表,以查看是否有新的文件添加。

相關問題