2013-02-02 52 views
0

我有上傳所有文件到ftp的問題:我用ftplib。 我有一個功能上傳:如何使用ftplib將目錄上傳到ftp?

static void DirSearch(string sDir, FtpConnection ftp) 
{ 
    try 
    { 
    foreach (string d in Directory.GetDirectories(sDir)) 
    { 
     string dirname = new DirectoryInfo(d).Name; 
     if (!ftp.DirectoryExists(dirname)) 
     { 
     ftp.CreateDirectory(dirname); 
     } 
     ftp.SetCurrentDirectory(dirname); 
     foreach (string f in Directory.GetFiles(d)) 
     { 
     Uri uri = new Uri(f);    
     ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath)); 

     } 
     DirSearch(d, ftp); 
    } 
    } 
    catch (System.Exception e) 
    { 
    MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error); 
    } 
} 

確定此功能uload文件,但我在本地磁盤文件:

UPLOAD 
--DIR1 
----DIR3 
------FILE4 
----FILE3 
--DIR2 
----DIR4 
------FILE7 
----FILE5 
----FILE6 
--FILE1 
--FILE2 

在serwer我:

UPLOAD 
--DIR1 
----DIR3 
------DIR2 
--------DIR4 
----------FILE7 
--------FILE5 
--------FILE6 
------FILE4 
----FILE3 

我沒有第一個文件夾和目錄樹中的文件是錯誤的 我認爲是排隊ftp.SetCurrentDirectory(dirname);

回答

2

那麼,你的功能是問題 - 當你進入該文件夾,複製文件到它,你不會回到以前的文件夾,而是你更深入樹木。

對於這個簡單的解決辦法是重寫這個功能,從目錄回去一旦通過它迭代:

static void DirSearch(string sDir, FtpConnection ftp) 
{ 
    try 
    { 
    // First, copy all files in the current directory 
    foreach (string f in Directory.GetFiles(d)) 
    { 
     Uri uri = new Uri(f);    
     ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath)); 
    } 

    // For all directories in the current directory, create directory if there is 
    // no such, and call this function recursively to copy files. 

    foreach (string d in Directory.GetDirectories(sDir)) 
    { 
     string dirname = new DirectoryInfo(d).Name; 
     if (!ftp.DirectoryExists(dirname)) 
     { 
     ftp.CreateDirectory(dirname); 
     } 
     ftp.SetCurrentDirectory(dirname); 
     DirSearch(d, ftp); 
    } 



    } 
    catch (System.Exception e) 
    { 
    MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error); 
    } 
    finally{ 
    // Go back! 
    ftp.SetCurrentDirectory(".."); //untested, but it should be fine, as I don't see cdup command in ftplib 
    } 

} 
1

是的,你說得對。您可以在每次通話時保存並分配當前目錄。試試這個:

static void DirSearch(string sDir, FtpConnection ftp, string currentDirectory) 
{ 
    try 
    { 
    ftp.SetCurrentDirectory(currentDirectory); 
    foreach (string d in Directory.GetDirectories(sDir)) 
    { 
     string dirname = new DirectoryInfo(d).Name; 
     if (!ftp.DirectoryExists(dirname)) 
     { 
     ftp.CreateDirectory(dirname); 
     }  
     foreach (string f in Directory.GetFiles(d)) 
     { 
     Uri uri = new Uri(f);    
     ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath)); 

     } 
     string newCurrentDir = currentDirectory + dirname + "/"; 
     DirSearch(d, ftp, newCurrentDir); 
    } 
    } 
    catch (System.Exception e) 
    { 
    MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error); 
    } 
} 

和方法調用

DirSearch("your initial dir", your ftp connection, "/"); 
0

此代碼是好的

static void DirSearch(string sDir, FtpConnection ftp, string currentDirectory) 
{ 
    try 
    { 
     ftp.SetCurrentDirectory(currentDirectory); 
     foreach (string f in Directory.GetFiles(sDir)) 
     { 
      Uri uri = new Uri(f); 
      ftp.PutFile(f, System.IO.Path.GetFileName(uri.LocalPath)); 

     } 
     foreach (string d in Directory.GetDirectories(sDir)) 
     { 
      ftp.SetCurrentDirectory(currentDirectory); 
      string dirname = new DirectoryInfo(d).Name; 
      if (!ftp.DirectoryExists(dirname)) 
      { 
       ftp.CreateDirectory(dirname); 
      } 

      string newCurrentDir = currentDirectory + "/" + dirname ; 
      DirSearch(d, ftp, newCurrentDir); 
     } 
    } 
    catch (System.Exception e) 
    { 
     MessageBox.Show(String.Format("Błąd FTP: {0} {1}", e.Message), "Błąd wysyłania plików na FTP", MessageBoxButton.OK, MessageBoxImage.Error); 
    } 
}