c# ftp 删除服务器 ABCTXT文件
您可以使用C#的FTP类来删除服务器上的文件。以下是一个示例代码,用于删除服务器上的/A/B/C.TXT文件:
using System;
using System.Net;
class Program
{
static void Main(string[] args)
{
string ftpServer = "ftp://example.com";
string filePath = "/A/B/C.TXT";
string username = "ftpUsername";
string password = "ftpPassword";
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpServer + filePath);
request.Method = WebRequestMethods.Ftp.DeleteFile;
request.Credentials = new NetworkCredential(username, password);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Console.WriteLine("File deleted successfully");
response.Close();
}
catch (WebException ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}
请确保将ftpServer替换为实际的FTP服务器地址,filePath替换为要删除的文件路径,以及提供正确的FTP用户名和密码。
该代码使用FtpWebRequest类来创建一个FTP请求,并设置请求的方法为DeleteFile以删除文件。然后,使用提供的用户名和密码进行身份验证,并发送请求以删除文件。
如果操作成功,将打印出"File deleted successfully"消息。如果出现错误,将打印出错误消息
原文地址: https://www.cveoy.top/t/topic/h1t0 著作权归作者所有。请勿转载和采集!