0

我在将所有文件上传到 ftp 时遇到问题:我使用ftplib。我有一个上传功能:

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

在服务器中,我有:

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

我在第一个文件夹中没有文件并且目录树是错误的

4

3 回答 3

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
  }

}
于 2013-02-02T13:24:38.203 回答
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, "/");
于 2013-02-02T13:24:17.933 回答
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);
    }
}
于 2013-02-02T14:50:18.993 回答