use*_*117 14 .net c# ftp webclient delete-file
我的程序可以使用以下代码将文件上传到FTP服务器:
WebClient client = new WebClient();
client.Credentials = new System.Net.NetworkCredential(ftpUsername, ftpPassword);
client.BaseAddress = ftpServer;
client.UploadFile(fileToUpload, WebRequestMethods.Ftp.UploadFile, fileName);
Run Code Online (Sandbox Code Playgroud)
现在我需要删除一些文件,我不能这样做.我该怎么用而不是
client.UploadFile(fileToUpload, WebRequestMethods.Ftp.UploadFile, fileName);
Run Code Online (Sandbox Code Playgroud)
Gra*_*ray 42
我想你需要使用FtpWebRequest类来做那个.
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();
Run Code Online (Sandbox Code Playgroud)
Ton*_*oda 12
public static bool DeleteFileOnFtpServer(Uri serverUri, string ftpUsername, string ftpPassword)
{
try
{
// The serverUri parameter should use the ftp:// scheme.
// It contains the name of the server file that is to be deleted.
// Example: ftp://contoso.com/someFile.txt.
//
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return false;
}
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
request.Method = WebRequestMethods.Ftp.DeleteFile;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
//Console.WriteLine("Delete status: {0}", response.StatusDescription);
response.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
DeleteFileOnFtpServer(new Uri (toDelFname), user,pass);
Run Code Online (Sandbox Code Playgroud)