.NET(C#)是否内置了FTP库?我不需要任何疯狂......非常简单.
我需要:
FTP进入一个帐户
检测连接是否被拒绝
获取文本文件
删除文本文件
最简单的方法是什么?
使用FtpWebRequest类或普通的旧WebClient类.
FTP进入帐户并检索文件:
WebClient request = new WebClient(); request.Credentials = new NetworkCredential("anonymous", "janeDoe@contoso.com"); try { // serverUri here uses the FTP scheme ("ftp://"). byte[] newFileData = request.DownloadData(serverUri.ToString()); string fileString = Encoding.UTF8.GetString(newFileData); } catch (WebException ex) { // Detect and handle login failures etc here }
删除文件:
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri); request.Method = WebRequestMethods.Ftp.DeleteFile; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); Console.WriteLine("Delete status: {0}", response.StatusDescription); response.Close();
(代码示例来自MSDN.)