在 C# 中从 FTP 中删除文件

时间:2023-03-31
本文介绍了在 C# 中从 FTP 中删除文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的程序可以使用以下代码将文件上传到 FTP 服务器:

My program can upload files into an FTP server using this code:

WebClient client = new WebClient();
client.Credentials = new System.Net.NetworkCredential(ftpUsername, ftpPassword);
client.BaseAddress = ftpServer;
client.UploadFile(fileToUpload, WebRequestMethods.Ftp.UploadFile, fileName);

现在我需要删除一些文件,但我做不到.我应该用什么来代替

Right now I need to delete some files and I can't do that right. What should I use instead of

client.UploadFile(fileToUpload, WebRequestMethods.Ftp.UploadFile, fileName);

推荐答案

您需要使用 FtpWebRequest 类来做那个,我想.

You'll need to use the FtpWebRequest class to do that one, I think.

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);

//If you need to use network credentials
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword); 
//additionally, if you want to use the current user's network credentials, just use:
//System.Net.CredentialCache.DefaultNetworkCredentials

request.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("Delete status: {0}", response.StatusDescription);  
response.Close();

这篇关于在 C# 中从 FTP 中删除文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:使用 C# 上传到服务器后,Zip 文件损坏 下一篇:如何在 FTP 服务器上复制文件?

相关文章