2009/04/02

c# FTP的使用 -- 取得FTP Server的檔案列表(ls)

// Get file list
public string[] FtpDir()
{
string Result = "";
if (this.InputValidation())
{
try
{
Uri UriFtpDir = new Uri("ftp://" + serverhost + ":" + serverport + dirpath + filter);
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UriFtpDir);
request.Method = WebRequestMethods.Ftp.ListDirectory;


request.Credentials = new NetworkCredential(username, password);

FtpWebResponse response = (FtpWebResponse)request.GetResponse();

StreamReader MyStreamReader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
returnmsg = response.BannerMessage;
returncode = response.StatusCode.ToString();

Result = MyStreamReader.ReadToEnd();
}
catch (Exception ex)
{
returnmsg = ex.ToString();
returncode = "0";
throw new ArgumentException(returnmsg);
}

}
else
{
throw new ArgumentException(returnmsg);
}
return Strings.StringToLines(Result);
}

沒有留言: