Deleting file from FTP in C#

后端 未结 4 951
小蘑菇
小蘑菇 2020-12-31 04:15

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

WebClient client = new WebClient();
client.Credentials = new System.Net.NetworkCredential(ftp         


        
相关标签:
4条回答
  • 2020-12-31 04:50
    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;
        }            
    }
    

    Usage:

    DeleteFileOnFtpServer(new Uri (toDelFname), user,pass);
    
    0 讨论(0)
  • 2020-12-31 05:05
    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;
    }
    
    0 讨论(0)
  • 2020-12-31 05:07

    You should use FtpWebRequest when you need to delete files:

    // 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();
    

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

    0 讨论(0)
  • 2020-12-31 05:11

    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();
    
    0 讨论(0)
提交回复
热议问题