public static void UploadFromLocalStorage(FTpInfo ftpInfo) { var fileName = Path.GetFileName(ftpInfo.FileName); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(string.Format("{0}/{1}", ftpInfo.Host, fileName)); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential(ftpInfo.UserName, ftpInfo.PassWord); request.UsePassive = true; request.UseBinary = true; request.KeepAlive = false; using (var fileStream = File.OpenRead(ftpInfo.FileName)) { using (var requestStream = request.GetRequestStream()) { fileStream.CopyTo(requestStream); requestStream.Close(); } } var response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Upload done: {0}", response.StatusDescription); response.Close(); }
static void Main(string[] args) { FTpInfo ftpInfo = new FTpInfo(); ////TestFTPServer(ftpInfo); ////UploadFromLocalStorage(ftpInfo); ////UploadFromStream(ftpInfo); UploadFromDicStream(ftpInfo); }
public static void TestFTPServer(FTpInfo ftpInfo) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpInfo.Host); request.Method = WebRequestMethods.Ftp.ListDirectory; request.Credentials = new NetworkCredential(ftpInfo.UserName, ftpInfo.PassWord); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); List<string> result = new List<string>(); while (!reader.EndOfStream) { result.Add(reader.ReadLine()); } reader.Close(); response.Close(); foreach (var item in result) { Console.WriteLine(item); } }
public static void TestFTPServer(FTpInfo ftpInfo) { FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpInfo.Host); request.Method = WebRequestMethods.Ftp.ListDirectory; request.Credentials = new NetworkCredential(ftpInfo.UserName, ftpInfo.PassWord); FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); List <string> result = new List <string>(); while (!reader.EndOfStream) { result.Add(reader.ReadLine()); } reader.Close(); response.Close(); foreach (var item in result) { Console.WriteLine(item); } }
public static void UploadFromDicStream(FTpInfo ftpInfo) { var fileName = Path.GetFileName(ftpInfo.FileName); FtpWebRequest request = (FtpWebRequest)WebRequest.Create(string.Format("{0}/{1}", ftpInfo.Host, fileName)); request.Method = WebRequestMethods.Ftp.UploadFile; request.Credentials = new NetworkCredential(ftpInfo.UserName, ftpInfo.PassWord); request.UsePassive = true; request.UseBinary = true; request.KeepAlive = false; Stream stream = GetStream(); using (var requestStream = request.GetRequestStream()) { stream.CopyTo(requestStream); requestStream.Close(); } var response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Upload done: {0}", response.StatusDescription); response.Close(); }