Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete remote files?

Tags:

c#

I have files that I want to delete. Connection can be from file sharing, http, and ftp.

Example of files to delete:

//mytest//delete//filename.bin
ftp://mytest/delete/filename.bin
http://mytest/delete/filename.bin

Here's what I did:

Uri target = new Uri(@"ftp://mytest/delete/filename.bin");
FileInfo fi = new FileInfo(target.AbsoluteUri);
fi.Delete();

The error I get is:

The given paths format is not supported

Is there a single code that can delete in all these file types?

I have created a simple code for this task(based on thread response).
This is the input:

Uri target = new Uri(@"ftp://tabletijam/FileServer/upload.bin");
Uri target = new Uri(@"http://tabletijam/FileServer/upload.bin");
Uri target = new Uri(@"\\tabletijam\FileServer\upload.bin");

This is the code:

bool DeleteFileOnServer(Uri serverUri)
{
    if (serverUri.Scheme == Uri.UriSchemeFtp)
    {
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
        request.Method = WebRequestMethods.Ftp.DeleteFile;

        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        lblStatus.Content = response.StatusDescription;

        response.Close(); 

        return true;
    }
    else if (serverUri.Scheme == Uri.UriSchemeFile)
    {
        System.IO.File.Delete(serverUri.LocalPath);

        return true;
    }
    else if (serverUri.Scheme == Uri.UriSchemeHttp || serverUri.Scheme == Uri.UriSchemeHttps)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serverUri);
        request.Method = WebRequestMethods.Http.DeleteFile;

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        lblStatus.Content = response.StatusDescription;

        response.Close();

        return true;
    }
    else
    {
        lblStatus.Content = "Unknown uri scheme.";
        return false;
    }
}

Ftp and File deleted successfully. WebRequestMethods.Http does not contain DeleteFile.

So my question is, how do I delete file from this URI?

http://tabletijam/FileServer/upload.bin
like image 504
Syaiful Nizam Yahya Avatar asked Mar 17 '11 09:03

Syaiful Nizam Yahya


People also ask

How do I delete a file from a remote branch?

Using git rm <deleted-filename> and git add <deleted-filename>. Your branch is up-to-date with 'origin/master'. It will stage the deleted file, and followed by git commit and git push will remove the file from the repository.

How do I delete a file remotely using SSH?

Sometimes you would need to remove a file or a folder from the system. To do so using SSH, you would need to execute the appropriate command – rm. This will match all files starting with 'myFile' and ending in '. txt' and delete them.


4 Answers

Because FileInfo only works with local files. For each connection you will need a special implementation.

For FTP: (example from MSDN)

public static bool DeleteFileOnServer(Uri serverUri)
{
    // 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.Method = WebRequestMethods.Ftp.DeleteFile;

    FtpWebResponse response = (FtpWebResponse) request.GetResponse();
    Console.WriteLine("Delete status: {0}",response.StatusDescription);  
    response.Close();
    return true;
}
like image 105
RvdK Avatar answered Nov 16 '22 13:11

RvdK


Using the \\server... notation, you can delete file (that you have access) to on remote servers.

Using FTP, you should the FtpWebRequest.

For HTTP, you could issue a DELETE request, using HttpWebRequest.

For both FTP and HTTP, you might need to supply a username and password. Also normally HTTP servers are not configured to delete files when receiving a DELETE request by default.

like image 24
GvS Avatar answered Nov 16 '22 11:11

GvS


For a whole lot of reasons, no, there is not a single unified way you can delete files via each of these protocols.

You could abstract this away into some implementation of your own, however, using an implementation specific to each of the protocols you want to support...

like image 43
tomfanning Avatar answered Nov 16 '22 11:11

tomfanning


how do i delete file from this uri?

request.Method = "DELETE";

Also, there's a different header supported by WebDAV for controlling the delete...

like image 32
George Tsiokos Avatar answered Nov 16 '22 12:11

George Tsiokos