Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting file from FTP in C#

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

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

Right now I need to delete some files and I can't do that right. What should I use instead of

client.UploadFile(fileToUpload, WebRequestMethods.Ftp.UploadFile, fileName);
like image 428
user2399117 Avatar asked Jul 12 '13 14:07

user2399117


People also ask

Can I delete from FTP?

You can use the FTP_DELETE and SFTP_DELETE objects to delete files from an FTP or SFTP server. You can delete all files from a folder, delete specific files from a folder, or delete specific files using the File_Path field. .

How can we delete a file in C#?

Delete(String) is an inbuilt File class method which is used to delete the specified file. Syntax: public static void Delete (string path);

How do I delete a file from an FTP server in Unix?

Type help to list all available commands that you can run on an ftp server. Type ls to list all files and directories. Type cd <folder_name> to get into a specified folder. type mdelete <filename> to delete a file or multiple files or rmdir -r <folder_name> to delete a folder.


2 Answers

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();
like image 153
Gray Avatar answered Sep 22 '22 09:09

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);
like image 36
Tone Škoda Avatar answered Sep 21 '22 09:09

Tone Škoda