-
c#一个FTP操作封装类FTPHelper
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net;
- using System.IO;
-
- public class FTPHelper
- {
-
-
-
- FtpWebRequest request = null;
-
-
-
- FtpWebResponse response = null;
-
-
-
- public string ftpURI { get; private set; }
-
-
-
- public string ftpServerIP { get; private set; }
-
-
-
- public string ftpRemotePath { get; private set; }
-
-
-
- public string ftpUserID { get; private set; }
-
-
-
- public string ftpPassword { get; private set; }
-
-
-
-
-
-
-
-
- public FTPHelper(string ftpServerIP, string ftpRemotePath, string ftpUserID, string ftpPassword)
- {
- this.ftpServerIP = ftpServerIP;
- this.ftpRemotePath = ftpRemotePath;
- this.ftpUserID = ftpUserID;
- this.ftpPassword = ftpPassword;
- this.ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
- }
- ~FTPHelper()
- {
- if (response != null)
- {
- response.Close();
- response = null;
- }
- if (request != null)
- {
- request.Abort();
- request = null;
- }
- }
-
-
-
-
-
-
- private FtpWebResponse Open(Uri uri, string ftpMethod)
- {
- request = (FtpWebRequest)FtpWebRequest.Create(uri);
- request.Method = ftpMethod;
- request.UseBinary = true;
- request.KeepAlive = false;
- request.Credentials = new NetworkCredential(this.ftpUserID, this.ftpPassword);
- return (FtpWebResponse)request.GetResponse();
- }
-
-
-
-
-
-
- private FtpWebRequest OpenRequest(Uri uri, string ftpMethod)
- {
- request = (FtpWebRequest)WebRequest.Create(uri);
- request.Method = ftpMethod;
- request.UseBinary = true;
- request.KeepAlive = false;
- request.Credentials = new NetworkCredential(this.ftpUserID, this.ftpPassword);
- return request;
- }
-
-
-
-
- public void CreateDirectory(string remoteDirectoryName)
- {
- response = Open(new Uri(ftpURI + remoteDirectoryName), WebRequestMethods.Ftp.MakeDirectory);
- }
-
-
-
-
-
- public void ReName(string currentName, string newName)
- {
- request = OpenRequest(new Uri(ftpURI + currentName), WebRequestMethods.Ftp.Rename);
- request.RenameTo = newName;
- response = (FtpWebResponse)request.GetResponse();
- }
-
-
-
-
- public void GotoDirectory(string DirectoryName, bool IsRoot)
- {
- if (IsRoot)
- ftpRemotePath = DirectoryName;
- else
- ftpRemotePath += "/" + DirectoryName;
-
- ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
- }
-
-
-
-
-
- public void RemoveDirectory(string remoteDirectoryName)
- {
- GotoDirectory(remoteDirectoryName, true);
- var listAll = ListFilesAndDirectories();
- foreach (var m in listAll)
- {
- if (m.IsDirectory)
- RemoveDirectory(m.Path);
- else
- DeleteFile(m.Name);
- }
- GotoDirectory(remoteDirectoryName, true);
- response = Open(new Uri(ftpURI), WebRequestMethods.Ftp.RemoveDirectory);
- }
-
-
-
-
- public void Upload(string localFilePath)
- {
- FileInfo fileInf = new FileInfo(localFilePath);
- request = OpenRequest(new Uri(ftpURI + fileInf.Name), WebRequestMethods.Ftp.UploadFile);
- request.ContentLength = fileInf.Length;
- int buffLength = 2048;
- byte[] buff = new byte[buffLength];
- int contentLen;
- using (var fs = fileInf.OpenRead())
- {
- using (var strm = request.GetRequestStream())
- {
- contentLen = fs.Read(buff, 0, buffLength);
- while (contentLen != 0)
- {
- strm.Write(buff, 0, contentLen);
- contentLen = fs.Read(buff, 0, buffLength);
- }
- }
- }
- }
-
-
-
-
- public void DeleteFile(string remoteFileName)
- {
- response = Open(new Uri(ftpURI + remoteFileName), WebRequestMethods.Ftp.DeleteFile);
- }
-
-
-
-
-
- public List<FileStruct> ListFilesAndDirectories()
- {
- var fileList = new List<FileStruct>();
- response = Open(new Uri(ftpURI), WebRequestMethods.Ftp.ListDirectoryDetails);
- using (var stream = response.GetResponseStream())
- {
- using (var sr = new StreamReader(stream))
- {
- string line = null;
- while ((line = sr.ReadLine()) != null)
- {
-
-
-
- DateTime dtDate = DateTime.ParseExact(line.Substring(0, 8), "MM-dd-yy", null);
- DateTime dtDateTime = DateTime.Parse(dtDate.ToString("yyyy-MM-dd") + line.Substring(8, 9));
- string[] arrs = line.Split(' ');
- var model = new FileStruct()
- {
- IsDirectory = line.IndexOf("<DIR>") > 0 ? true : false,
- CreateTime = dtDateTime,
- Name = arrs[arrs.Length - 1],
- Path = ftpRemotePath + "/" + arrs[arrs.Length - 1]
- };
- fileList.Add(model);
- }
- }
- }
- return fileList;
- }
-
-
-
- public List<FileStruct> ListFiles()
- {
- var listAll = ListFilesAndDirectories();
- var listFile = listAll.Where(m => m.IsDirectory == false).ToList();
- return listFile;
- }
-
-
-
- public List<FileStruct> ListDirectories()
- {
- var listAll = ListFilesAndDirectories();
- var listFile = listAll.Where(m => m.IsDirectory == true).ToList();
- return listFile;
- }
-
-
-
-
- public bool IsExist(string remoteName)
- {
- var list = ListFilesAndDirectories();
- if (list.Count(m => m.Name == remoteName) > 0)
- return true;
- return false;
- }
-
-
-
-
- public bool IsDirectoryExist(string remoteDirectoryName)
- {
- var listDir = ListDirectories();
- if (listDir.Count(m => m.Name == remoteDirectoryName) > 0)
- return true;
- return false;
- }
-
-
-
-
- public bool IsFileExist(string remoteFileName)
- {
- var listFile = ListFiles();
- if (listFile.Count(m => m.Name == remoteFileName) > 0)
- return true;
- return false;
- }
-
-
-
-
-
-
- public void Download(string saveFilePath, string downloadFileName)
- {
- using (FileStream outputStream = new FileStream(saveFilePath + "\" + downloadFileName, FileMode.Create))
- {
- response = Open(new Uri(ftpURI + downloadFileName), WebRequestMethods.Ftp.DownloadFile);
- using (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);
- }
- }
- }
- }
-
-
- }
-
- public class FileStruct
- {
-
-
-
- public bool IsDirectory { get; set; }
-
-
-
- public DateTime CreateTime { get; set; }
-
-
-
- public string Name { get; set; }
-
-
-
- public string Path { get; set; }
- }
-
相关阅读:
Java io 理解
Java应用的理解
Flyweight 享元模式
Bridge 桥梁模式
Decrator 装饰模式
[CF997C]Sky Full of Stars_二项式反演_等比数列_容斥原理
[CF1010D]Mars Over_位运算性质
[CF991D]Bishwock_状压dp
[Agc030B]Tree Burning_贪心
[Cometoj#4 E]公共子序列_贪心_树状数组_动态规划
-
原文地址:https://www.cnblogs.com/lvdongjie/p/5600591.html
Copyright © 2020-2023
润新知