现在做的一个项目是用的silverlight做的。 但是遇到一个问题 就是FTP下载的时候 获取GetResponse时 是超时的响应。 在百度上搜了很多的资料都没有解决呢。 结果发现解决其实很简单。 见下测红色代码。
//FTP下载操作
public bool getFile(string FTP, string Account, string Password,string FileName,string LocalPath)
{
FtpWebRequest reqFTP;
try
{
FileStream outputStream = new FileStream(LocalPath, FileMode.Create);
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + FTP + "/" + FileName));
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.UsePassive = true;
reqFTP.KeepAlive = true;
reqFTP.Credentials = new NetworkCredential(Account, Password);
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);
while (readCount > 0)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
return true;
}
catch (Exception err)
{
ExceptionRecord("FTP下载", err.Message + "\r\n" + err.StackTrace);
return false;
}
}
这个样子的FTP函数。 就能在c#中使用。 同时也能被silverlight的wcf层进行引用了~