*.xml の拡張子を持つすべてのファイル名を FTP サーバーから読み取り、文字列ファイル名の配列を返すメソッドを作成しました。しかし、「指定されたパスの形式はサポートされていません..」というエラーが表示されます。コード スニペットは次のとおりです。
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;
どうすればこれを達成できますか? あなたの助けに感謝します...