读取FTP上的某个文本文档内容到本地

/// <summary>
/// 读取FTP服务器文本内容
/// </summary>
/// <param name="strPath">地址</param>
/// <param name="strUserName">账号</param>
/// <param name="strPassword">密码</param>
/// <returns></returns>
public List<string> GetFileStr(string ftpPath, string ftpUserName, string ftpPassword)
{
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpPath));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpPassword);
reqFTP.UsePassive = false; //选择主动还是被动模式 。
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;//不设置此属性一次性下载多个文件的时候,会出现异常。
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
Stream ftpStream = response.GetResponseStream();
string line = reader.ReadToEnd();
List<string> EmployeeData = line.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();//根据换行符拆分到list中
reader.Close();
ftpStream.Close();
response.Close();
return EmployeeData;
}
catch (Exception ex)
{
return null;
}
}

posted @ 2018-03-27 15:15  举个栗子oO  阅读(1741)  评论(0编辑  收藏  举报