10

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
tshepang
  • 12,111
  • 21
  • 91
  • 136
Syaiful Nizam Yahya
  • 4,196
  • 11
  • 51
  • 71
  • you can't directly request the fileinfo like this. You need to make an actual request to the location where your files a placed. Either with an web- or ftp request. Then you can get the fileinfo and delete the files. – Rob Mar 17 '11 at 09:42

5 Answers5

16

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;
}
RvdK
  • 19,580
  • 4
  • 64
  • 107
2

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.

GvS
  • 52,015
  • 16
  • 101
  • 139
1

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...

tomfanning
  • 9,552
  • 4
  • 50
  • 78
1

how do i delete file from this uri?

request.Method = "DELETE";

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

George Tsiokos
  • 1,890
  • 21
  • 31
-1

No, this is not possible. FTP and HTTP are protocols that you are required to stick to. Even though you may be able to delete files when viewing FTP folders in the Explorer doesn't mean it works from C#, too, because the Explorer uses an integrated FTP client. Deleting files via HTTP like that isn't possible at all.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • He asked three questions: "(how do you) Delete remote files?", "Is there a single code that can delete in all these file types?", and "how do I delete (a) file from this URI?". Their are solutions to each of these questions (albeit the second question is more of a logic error on his part) and none should be answered with "No, this is not possible.". Also your last statement about deleting with HTTP being impossible is also wrong. Deleting is possible via HTTP. – Brennen Sprimont Jul 27 '15 at 17:52
  • @BrennenSprimont well, my answer says the same thing as the others and it is correct - so I don't see a reason to downvote really. – Thorsten Dittmar Jul 27 '15 at 18:16
  • It is not the same, none of the other answers say "this is not possible". They all say it is possible and then explain how to do it. If you where to delete your first and last statements then it would be a bearable answer and I would remove my down vote. But as it stands, with those two statements, your answer is incorrect. – Brennen Sprimont Jul 27 '15 at 18:24
  • "Is there a single code that can delete in all these file types?" Answer: no, you can not do this, you need to adhere to the protocols. What is unclear or wrong about this? The other answers don't say it is possible. They do describe how to remove files via FTP or shares, but none of the answers provides a single "in one go" command. So please go and downvote them all. By the way, even the accepted answer says the same thing, only providing an FTP sample. Also, this is the only correct answer. You can delete all these files, just not in one go, but protocol specific. – Thorsten Dittmar Jul 27 '15 at 19:23
  • Also if you look at the edit history of the question you'll notice I answered the original question, not the edited one. And my answer answers that question validly. – Thorsten Dittmar Jul 27 '15 at 19:45
  • That would be a better answer, unfortunately you did not add that clarification into your answer. It's impossible to tell how you interpreted his question without giving any detail. How is anyone supposed to know you are answering his second question? and not his first (the actual title) or third? This along with actually giving helpful advice on how to implement a solution is what separates your question from the rest. Your second statement is also false, he also can map the remote server drive, or even implement a server socket solution. – Brennen Sprimont Jul 27 '15 at 19:50
  • The question is from 2011. Do you really expect anyone to monitor all the questions he answered for potential edits? Also, my answer remains as is. The framework does not provide a one in all solution to this problem. Please go downvote all the other answers too if you feel like it. – Thorsten Dittmar Jul 27 '15 at 19:55
  • Well therein-lies one of the fundamental problems with a Q&A system. But as it stands, with the question updated, your answer is not suitable anymore. The past status of the question is irrelevant now. Stop taking this as a slight to your character, the question as it stands is not answered by your answer anymore so I down-voted it to not confuse further members of the community who are looking for insight on "Deleting remote files". A new member could easily infer from your answer that FTP is the only way to accomplish deleting remote files and the voting system helps prevent that. – Brennen Sprimont Jul 27 '15 at 20:04