web-dev-qa-db-fra.com

Supprimer un fichier de FTP en C #

Mon programme peut télécharger des fichiers sur un serveur FTP en utilisant ce code:

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

Pour le moment, je dois supprimer certains fichiers et je ne peux pas le faire correctement. Que dois-je utiliser au lieu de

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

Vous aurez besoin d'utiliser la classe FtpWebRequest pour faire celle-là, je pense.

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();
44
Gray
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);
13
Tone Škoda
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;
}
3
Emre Erol

Vous devez utiliser FtpWebRequest lorsque vous devez supprimer des fichiers:

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

réf: http://msdn.Microsoft.com/en-us/library/system.net.ftpwebrequest.aspx

2
Ralf de Kleine