<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
實現使用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操作工具的詳細內容,更多關於C# FTP操作工具的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45