您好,登錄后才能下訂單哦!
這篇文章主要介紹“基于C#如何實現一個簡單的FTP操作工具”,在日常操作中,相信很多人在基于C#如何實現一個簡單的FTP操作工具問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”基于C#如何實現一個簡單的FTP操作工具”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
實現使用FTP上傳、下載、重命名、刷新、刪除功能
開發工具: Visual Studio 2013
.NET Framework版本:4.5
/*FTP操作公共類*/ private string FtpIp, FtpPort, FtpUser, FtpPwd, FtpUrl; private FTPUtil() { } public FTPUtil(string ftpIp, string ftpPort, string ftpUser, string ftpPwd) { FtpIp = ftpIp; FtpPort = ftpPort; FtpUser = ftpUser; FtpPwd = ftpPwd; FtpUrl = "ftp://" + ftpIp + ":" + ftpPort + "/"; } private FtpWebRequest GetFtpWebRequest(string path, string method) { FtpWebRequest Ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(FtpUrl + "/" + path)); Ftp.Credentials = new NetworkCredential(FtpUser, FtpPwd); Ftp.KeepAlive = false; Ftp.UsePassive = true; Ftp.Method = method; return Ftp; } /// <summary> /// 獲取路徑下所有文件夾 /// </summary> /// <param name="dirName"></param> /// <returns></returns> public List<FileModel> GetDirs(string dirName) { return GetAllFiles(dirName).FindAll(s => s.Type == "文件夾"); } /// <summary> /// 獲取路徑下所有文件 /// </summary> /// <param name="dirName"></param> /// <returns></returns> public List<FileModel> GetFiles(string dirName) { return GetAllFiles(dirName).FindAll(s => s.Type == "文件"); } /// <summary> /// 獲取路徑下所有項目 /// </summary> /// <param name="dirName"></param> /// <returns></returns> public List<FileModel> GetAllFiles(string dirName) { List<FileModel> fileList = new List<FileModel>(); try { FtpWebRequest Ftp = GetFtpWebRequest(dirName, WebRequestMethods.Ftp.ListDirectoryDetails); using (WebResponse response = Ftp.GetResponse()) { using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { string line = ""; while ((line = reader.ReadLine()) != null) { fileList.Add(ConvertFile(line, dirName)); } } } } catch (Exception ex) { throw ex; } return fileList; } /// <summary> /// FTP文件信息轉換 /// </summary> /// <param name="value"></param> /// <param name="dirName"></param> /// <returns></returns> private FileModel ConvertFile(string value, string dirName) { string[] arr = value.Split(new string[] { " " },4, StringSplitOptions.RemoveEmptyEntries); FileModel model = new FileModel(); model.Date = arr[0]; model.Time = arr[1]; if (arr[2] == "<DIR>") { model.Type = "文件夾"; model.Size = 0; } else { model.Type = "文件"; model.Size = Convert.ToInt64(arr[2]); } model.Name = arr[3]; model.FullName = dirName + "/" + model.Name; return model; } /// <summary> /// 上傳 /// </summary> /// <param name="fileName"></param> /// <param name="desFile"></param> public void Upload(string fileName, string desFile) { try { FileInfo fileInfo = new FileInfo(fileName); FtpWebRequest Ftp = GetFtpWebRequest(desFile, WebRequestMethods.Ftp.UploadFile); Ftp.UseBinary = true; Ftp.ContentLength = fileInfo.Length; int buffLength = 2048; byte[] buff = new byte[buffLength]; int len = 0; using (FileStream fs = fileInfo.OpenRead()) { using (Stream stream = Ftp.GetRequestStream()) { while ((len = fs.Read(buff, 0, buffLength)) != 0) { stream.Write(buff, 0, buffLength); } } } } catch (Exception ex) { throw ex; } } /// <summary> /// 下載 /// </summary> /// <param name="fileName"></param> /// <param name="desFile"></param> public void DownLoad(string fileName, string desFile) { try { FtpWebRequest Ftp = GetFtpWebRequest(fileName, WebRequestMethods.Ftp.DownloadFile); Ftp.UseBinary = true; FtpWebResponse response = (FtpWebResponse)Ftp.GetResponse(); int buffLength = 2048; byte[] buff = new byte[buffLength]; int len = 0; using (FileStream fs = new FileStream(desFile, FileMode.Create)) { using (Stream stream = response.GetResponseStream()) { while ((len = stream.Read(buff, 0, buffLength)) != 0) { fs.Write(buff, 0, buffLength); } } } } catch (Exception ex) { throw ex; } } /// <summary> /// 刪除文件 /// </summary> /// <param name="fileName"></param> public void DeleteFile(string fileName) { try { FtpWebRequest Ftp = GetFtpWebRequest(fileName, WebRequestMethods.Ftp.DeleteFile); FtpWebResponse response = (FtpWebResponse)Ftp.GetResponse(); using (Stream datastream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(datastream)) { sr.ReadToEnd(); } } } catch (Exception ex) { throw ex; } } /// <summary> /// 重命名 /// </summary> /// <param name="fileName"></param> /// <param name="newName"></param> public void ReName(string fileName, string newName) { try { FtpWebRequest Ftp = GetFtpWebRequest(fileName, WebRequestMethods.Ftp.Rename); Ftp.RenameTo = newName; Ftp.UseBinary = true; FtpWebResponse response = (FtpWebResponse)Ftp.GetResponse(); using (Stream datastream = response.GetResponseStream()) { using (StreamReader sr = new StreamReader(datastream)) { sr.ReadToEnd(); } } } catch (Exception ex) { throw ex; } }
到此,關于“基于C#如何實現一個簡單的FTP操作工具”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。