1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
|
using System;
using System.IO;
using System.Net;
using System.Collections.Generic;
using System.Text;
namespace FtpClient
{
public class cFtpClient
{
public Uri Address { get; set; }
public string Login { get; set; }
public string Password { get; set; }
public string[] List()
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(Address);
request.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
request.Credentials = new NetworkCredential(Login, Password);
request.UsePassive = true;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string[] ret = reader.ReadToEnd().Split('\n');
List<string> res = new List<string>();
for (int i = 0, cpt = ret.Length; i < cpt; i++)
{
if (ret[i].Length > 67)
{
res.Add(ret[i].Substring(67).Trim());
}
}
reader.Close();
return res.ToArray();
}
public void Download(string file, string place)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(string.Format("{0}/{1}", Address.AbsoluteUri, file));
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.UseBinary = true;
request.Credentials = new NetworkCredential(Login, Password);
request.UsePassive = true;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writer = File.Create(string.Format("{0}{1}", place, file.Replace('\\', '_').Replace('/', '_').Replace('*', '_').Replace(':', '_').Replace('?', '_').Replace('"', '_').Replace('<', '_').Replace('>', '_').Replace('|', '_')));
byte[] chunk = new byte[4096];
int bytesRead;
while ((bytesRead = responseStream.Read(chunk, 0, chunk.Length)) > 0)
{
writer.Write(chunk, 0, bytesRead);
}
writer.Close();
}
}
} |
Partager