1
的服務器和返回列表讀取文件我創建了一個方法,使閱讀從FTP服務器上的.xml的*擴展名的所有文件名,並返回字符串的文件名的數組。但我得到了一些錯誤「給出的路徑的格式不支持。」這裏是代碼片段:的FtpWebRequest上的文件名
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;
}
}
一旦我得到陣列中的所有文件名,我會結合成組合框sitesXmlCombo對象這一點,像這樣:
Uri uri = new Uri(uriString);
string[] xmlFiles = FileList.GetFileList(uri, "username", "password");
sitesXmlCombo.DataSource = xmlFiles;
Cursor.Current = Cursors.Arrow;
我該如何做到這一點?我感謝您的幫助...