class FTP_Class
{
string ftpServerIP;
string ftpUserID;
string ftpPassword;
FtpWebRequest reqFTP;
public void Connecttest(string ftpServerIP, string ftpUserID, string ftpPassword)
{
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP));
// 指定数据传输类型
reqFTP.UseBinary = true;
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
}
#region 连接
/// <summary>
/// 连接
/// </summary>
/// <param name="path"></param>
private void Connect(String path)//连接ftp
{
// 根据uri创建FtpWebRequest对象
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path));
// 指定数据传输类型
reqFTP.UseBinary = true;
// ftp用户名和密码
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
}
#endregion
#region ftp登录信息
/// <summary>
/// ftp登录信息
/// </summary>
/// <param name="ftpServerIP">ftpServerIP</param>
/// <param name="ftpUserID">ftpUserID</param>
/// <param name="ftpPassword">ftpPassword</param>
public void FtpUpDown(string ftpServerIP, string ftpUserID, string ftpPassword)
{
this.ftpServerIP = ftpServerIP;
this.ftpUserID = ftpUserID;
this.ftpPassword = ftpPassword;
}
#endregion
#region 获取文件列表
/// <summary>
/// 获取文件列表
/// </summary>
/// <param name="path"></param>
/// <param name="WRMethods"></param>
/// <returns></returns>
private string[] GetFileList(string path, string WRMethods)//上面的代码示例了如何从ftp服务器上获得文件列表
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
try
{
Connect(path);
reqFTP.Method = WRMethods;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);//中文文件名
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("/n");
line = reader.ReadLine();
}
// to remove the trailing '/n'
result.Remove(result.ToString().LastIndexOf('/n'), 1);
reader.Close();
response.Close();
return result.ToString().Split('/n');
}
catch (Exception ex)
{
System.Windows.Forms.Mess