我正在使用以下代码从ftp服务器从目录(例如“ hedrabad”)下载ZIP文件。
尝试{
FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(Procedures.ftpurl);
ftpRequest.Credentials = new NetworkCredential(Procedures.ftpuser, Procedures.ftpPassword);
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
StreamReader streamReader = new StreamReader(response.GetResponseStream());
List<string> directories = new List<string>();
string line = streamReader.ReadLine();
while (!string.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();
using (WebClient ftpClient = new WebClient())
{
ftpClient.Credentials = new System.Net.NetworkCredential(Procedures.ftpuser, Procedures.ftpPassword);
for (int i = 0; i <= directories.Count - 1; i++)
{
//if (directories[i].Contains("."))
//{
if (directories[i].ToString() == "Hyderabad")
{
log.Info("File Downloding Start : " + DateTime.Now.ToString("HH:mm ss"));
if (!Directory.Exists(OGSync.FTPPath + "\\Backups\\" + directories[i].ToString()))
{
Directory.CreateDirectory(OGSync.FTPPath + "\\Backups\\" + directories[i].ToString());
}
//string path = Procedures.ftpurl;
string path = Procedures.ftpurl + directories[i].ToString();
string trnsfrpth = OGSync.FTPPath + "Backups\\" + directories[i].ToString();
//string trnsfrpth = @"E:\\Test\" + directories[i].ToString();
ftpClient.DownloadFile(path, trnsfrpth);
//ftpClient.DownloadFile(path, @"C:\file.zip");
}
// }
}
}
}
catch (Exception ex)
{
log.Error(ex.Message);
}
return true;
}
我遇到了我尝试过但未解决的错误。请提供任何可以解决此问题的想法。
例外:对路径'E:\ Zipfils \ Backups \ Hyderabad'的访问被拒绝。
内部异常:对路径'E:\ Zipfils \ Backups \ Hyderabad'的访问被拒绝。
StackTrace:位于System.Net.WebClient.DownloadFile(Uri地址,字符串fileName),位于E:\ Modular代码和Installer \ OGServerSync \ OGServerSync \ ServerSync.cs:第64行的OGServerSync.ServerSync.Downloadfile()处。 >
消息:WebClient请求期间发生了异常。