开发者

Create folder on ftp

开发者 https://www.devze.com 2023-02-02 04:10 出处:网络
I am using method to create folder on ftp i want get exception if folder already exsists how to make it over write the existing folder

I am using method to create folder on ftp i want get exception if folder already exsists how to make it over write the existing folder

using System; using System.Net;

class Test {
    static void Main()
    {
        WebRequest request = WebRequest.Create("ftp://host.com/directory");
        request.Method = WebRequestMethods.Ftp.MakeDirectory;
        request.Credentials = new Net开发者_如何学运维workCredential("user", "pass");
        using (var resp = (FtpWebResponse) request.GetResponse())
        {
            Console.WriteLine(resp.StatusCode);
        }
    } }

it is "remote server returned error (550) file not found"


Well, "I want to get exception if folder already exists" and "how to make it overwrite the existing folder" are two opposing questions.

At any rate, I just implemented code to do this the other day. Just check if the directory already exists first. And then respond based on that. There's no point in trying to create a directory that already exists.

And if you need to overwrite it somehow, then delete the existing directory before creating the new one.

You can see the code I wrote for this in the article An FtpClient Class and WinForm Control, although it will just overwrite existing content.


You might want to consider some existing ftp libs out there. I've been using this and have had great success with it. It's an FTP client library that provides high-level FTP functionality for the FTPrequest in the .NET Framework 2.0.

It has an API for checking if directory exists and for creating the directory.

Code @ http://ftpclient.codeplex.com/

Article @ http://www.codeproject.com/KB/IP/FtpClient.aspx


I use this func in solution

private void createFolder(string ftpUName, string ftpPWord)
    {
            WebRequest ftpRequest = WebRequest.Create("ftp://mrhotro.ad/new_sc");
            ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
            ftpRequest.Credentials = new NetworkCredential(ftpUName, ftpPWord);
    }


You can't overwrite an existing folder... I'm not even sure what benefit that might be.

In short, you need to capture the exception and decide what to do. Either leave it in place (after all, it already exists...) or you'll need to delete the existing folder and try to recreate it.

I don't remember if you can delete a folder that currently contains files.. If you can't, then you will have to delete those as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消