1

我创建了一个方法,可以从 FTP 服务器读取所有文件名,扩展名为 *.xml,并返回一个字符串文件名数组。但我收到一些错误“不支持给定路径的格式..”这是代码片段:

protected static string[] FTPRelativePaths(string[] filelist)
{
    if (null == filelist)
        return new string[0];

    string[] result = new string[filelist.Length];
    for (int index = 0; index < filelist.Length; ++index)
        result[index] = Path.GetFileName(filelist[index]);

    return result;
}

public static string[] GetFileList(Uri serverUri, string ftpUserID,
    string ftpPassword)
{
     // The serverUri parameter should start with the ftp:// scheme.
     if (serverUri.Scheme != Uri.UriSchemeFtp)
     {
         return null;
     }

     string[] fileList= new string[0];
     try
     {
         WebRequest.DefaultWebProxy = null;
         FtpWebRequest ftprequest =(FtpWebRequest)WebRequest.Create(serverUri);
         //ftprequest.UseBinary = true;
         ftprequest.Method = WebRequestMethods.Ftp.ListDirectory;
         ftprequest.Credentials =new NetworkCredential(ftpUserID, ftpPassword);
         FtpWebResponse response = ftprequest.GetResponse() as FtpWebResponse;
         string ext = "*.xml";
         string[] foundfiles = FTPRelativePaths(
             Directory.GetFiles(serverUri.ToString(), ext));

         if (foundfiles.Length > 0)
         {
             string[] newlist = new string[foundfiles.Length];
             foundfiles.CopyTo(newlist, fileList.Length);
             fileList = newlist;
             System.Array.Sort(fileList);
         }

         response.Close();
         return fileList;
     }
     catch (Exception ex)
     {
         System.Windows.Forms.MessageBox.Show(ex.Message);
         fileList = null;
         return fileList;
     }
 }

一旦我得到一个数组中的所有文件名,我将把它绑定到 ComboBox sitesXmlCombo 对象中,如下所示:

Uri uri = new Uri(uriString);               
string[] xmlFiles = FileList.GetFileList(uri, "username", "password");

sitesXmlCombo.DataSource = xmlFiles;
Cursor.Current = Cursors.Arrow;

我怎样才能做到这一点?感谢您的帮助...

4

1 回答 1

0

我认为您在 Uri 上有错误;试试这个:

string _remoteHost = "ftp://ftp.name.com/htdocs/directory/";
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(_remoteHost));
于 2012-09-21T10:17:26.980 回答