操作FTP方法:/// 获取文件夹内文件信息
/// </summary>
/// <param name="BaseUriStr">基Uri</param>
/// <param name="AimUriStr">目标目录,相对路径</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static List<string> ListFtpDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
//FtpRequest.KeepAlive = true;
FtpRequest.KeepAlive = false;
FtpRequest.ReadWriteTimeout = 10000;
FtpRequest.UsePassive = false;
FtpRequest.Proxy = null;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
StreamReader srd = new StreamReader(FtpResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string ResponseBackStr = srd.ReadToEnd();
srd.Close();
FtpResponse.Close(); string[] ListDetails = ResponseBackStr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
List<string> RtnList = new List<string>();
foreach (string ListDetail in ListDetails)
{
if (ListDetail.StartsWith("d") && (!ListDetail.EndsWith(".")))
{
string FtpDirName = ListDetail.Substring(ListDetail.IndexOf(':') + 3).TrimStart();
RtnList.Add(FtpDirName + "|D");
}
else if (ListDetail.StartsWith("-"))
{
string FtpDirName = ListDetail.Substring(ListDetail.IndexOf(':') + 3).TrimStart();
RtnList.Add(FtpDirName + "|F");
}
} return RtnList;
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
FtpResponse.Close(); return new List<string>();
}
}
返加一个list,保存该文件夹内所有文件名。循环执行这个程序,用于返回所有文件夹的所有文件文件名,循环第一遍没问题,第二遍时,获取另一个文件夹时,报错:
远程服务器返回错误: (550) 文件不可用(例如,未找到文件,无法访问文件)文件夹是存在的,因为就算执行2次这个信函数,参数不变,执行到第2次的时候也会报这个错误 ,不知道是什么原因引起的。谢谢大家帮忙 。
/// </summary>
/// <param name="BaseUriStr">基Uri</param>
/// <param name="AimUriStr">目标目录,相对路径</param>
/// <param name="UserName">用户名</param>
/// <param name="UserPwd">用户密码</param>
/// <returns></returns>
public static List<string> ListFtpDirectory(string BaseUriStr, string AimUriStr, string UserName, string UserPwd)
{
Uri BaseUri = new Uri(BaseUriStr);
Uri AimUri = new Uri(BaseUri, AimUriStr);
FtpWebRequest FtpRequest = (FtpWebRequest)WebRequest.Create(AimUri);
//FtpRequest.KeepAlive = true;
FtpRequest.KeepAlive = false;
FtpRequest.ReadWriteTimeout = 10000;
FtpRequest.UsePassive = false;
FtpRequest.Proxy = null;
NetworkCredential FtpCred = new NetworkCredential(UserName, UserPwd);
CredentialCache FtpCache = new CredentialCache();
FtpCache.Add(AimUri, AuthType.Basic.ToString(), FtpCred);
FtpRequest.Credentials = FtpCache;
FtpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; try
{
FtpWebResponse FtpResponse = (FtpWebResponse)FtpRequest.GetResponse();
StreamReader srd = new StreamReader(FtpResponse.GetResponseStream(), Encoding.GetEncoding("GB2312"));
string ResponseBackStr = srd.ReadToEnd();
srd.Close();
FtpResponse.Close(); string[] ListDetails = ResponseBackStr.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
List<string> RtnList = new List<string>();
foreach (string ListDetail in ListDetails)
{
if (ListDetail.StartsWith("d") && (!ListDetail.EndsWith(".")))
{
string FtpDirName = ListDetail.Substring(ListDetail.IndexOf(':') + 3).TrimStart();
RtnList.Add(FtpDirName + "|D");
}
else if (ListDetail.StartsWith("-"))
{
string FtpDirName = ListDetail.Substring(ListDetail.IndexOf(':') + 3).TrimStart();
RtnList.Add(FtpDirName + "|F");
}
} return RtnList;
}
catch (WebException e)
{
FtpWebResponse FtpResponse = (FtpWebResponse)e.Response;
FtpResponse.Close(); return new List<string>();
}
}
返加一个list,保存该文件夹内所有文件名。循环执行这个程序,用于返回所有文件夹的所有文件文件名,循环第一遍没问题,第二遍时,获取另一个文件夹时,报错:
远程服务器返回错误: (550) 文件不可用(例如,未找到文件,无法访问文件)文件夹是存在的,因为就算执行2次这个信函数,参数不变,执行到第2次的时候也会报这个错误 ,不知道是什么原因引起的。谢谢大家帮忙 。
解决方案 »
- 求.net解决方案
- 域和属性的区别?
- c# 总是提示上下文中不存在。。。
- 大家都给孩子起名 那我也凑个热闹
- sql server 数据有关问题,大家帮忙~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 在installshield2008里如何让用户输入:服务器名,用户名和密码;之后又如何使用Secured FTP把文件安装到远程服务器上?
- 微博 转发 功能 的简单数据库设计和页面设计
- 一个初级问题,在线等,马上给分
- 请教带闰年判断的正则表达式
- 关于数据库连接的两个经典问题
- 网站调试失败问题
- C#如何实现水印
C#封装的完整FTP类
public void DownLoadFile(string localFilePath, string serverFolder)
{
string uriString; uriString = serverFolder; // 服务器保存路径
/**/
/// 创建WebClient实例
WebClient myWebClient = new WebClient();
Uri uUri = new Uri(uriString);
// WEB 身份验证
NetworkCredential WebCred = new NetworkCredential("name", "pwd");
CredentialCache WebCache = new CredentialCache();
WebCache.Add(uUri, AuthType.Basic.ToString(), WebCred);
myWebClient.Credentials = WebCache;
myWebClient.Proxy = null;
try
{
myWebClient.DownloadFile (uUri, localFilePath);
myWebClient.Dispose();
}
catch (Exception err)
{
} }实在不知道该怎样解决了....每当调用上面方法从FTP下载文件后,再执行1楼的方法,就会报错。
文件是存在的,这点可以肯定,ListFtpDirectory(....) 函数参数不变,执行第一次没问题,第二次就报550。在此期间执行了FTP的文件下载操作,就会出现这个问题。
其实有点不好意思说,问题并没有真正解决,只是将WEBCLIEN下载文件,改用了相同的FtpWebRequest。
注意KeepAlive = false;否则同样会报550
我设置了还是会报错啊,我用的是递归遍历整个FTP取得所有文件.文件少的时候不出错,多了就出550错误.
即使设置了 request.KeepAlive = false;
收到LIST命令的Response数据,客户端没有发QUIT命令来结束FTP连接。
再次运行时就失败了。不知怎么解决
public bool Download(string filePath, string fileName, out string errorinfo)/**/////上面的代码实现了从ftp服务器下载文件的功能
{ try
{
string onlyFileName = Path.GetFileName(fileName);
//string newFileName = filePath + "\\" + onlyFileName;
string newFileName = "E:\\VMI\\" + onlyFileName;
if (File.Exists(newFileName))
{
errorinfo = string.Format("本地文件{0}已存在,无法下载", newFileName);
return false;
}
string url = "ftp://" + ftpServerIP + "/" +filePath+ "/"+fileName;
Connect(url);//连接 reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse(); Stream ftpStream = response.GetResponseStream(); long cl = response.ContentLength; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize);
FileStream outputStream = new FileStream(newFileName, FileMode.Create); while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize);
} ftpStream.Close(); outputStream.Close();
response.Close();
errorinfo = "";
return true;
}
catch (Exception ex)
{
errorinfo = string.Format("因{0},无法下载", ex.Message);
return false;
}
}