繁体   English   中英

在C#中从FTP删除文件

[英]Deleting file from FTP in C#

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

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

现在,我需要删除一些文件,而我不能这样做。 我应该用什么代替

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

我认为,您需要使用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();
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;
    }            
}

用法:

DeleteFileOnFtpServer(new Uri (toDelFname), user,pass);
public static bool DeleteFileOnServer(Uri serverUri)
{

if (serverUri.Scheme != Uri.UriSchemeFtp)
{
    return false;
}
// Get the object used to communicate with the server.
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();
return true;
}

需要删除文件时,应使用FtpWebRequest:

// Get the object used to communicate with the server.
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();

参考: http : //msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM