How do I create a directory on FTP server using C#?

前端 未结 4 1130
栀梦
栀梦 2020-11-29 01:36

What\'s an easy way to create a directory on an FTP server using C#?

I figured out how to upload a file to an already existing folder like this:

usi         


        
相关标签:
4条回答
  • 2020-11-29 01:44

    Something like this:

    // remoteUri points out an ftp address ("ftp://server/thefoldertocreate")
    WebRequest request = WebRequest.Create(remoteUri);
    request.Method = WebRequestMethods.Ftp.MakeDirectory;
    WebResponse response = request.GetResponse();
    

    (a bit late. how odd.)

    0 讨论(0)
  • 2020-11-29 01:46

    Use FtpWebRequest, with a method of WebRequestMethods.Ftp.MakeDirectory.

    For example:

    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 NetworkCredential("user", "pass");
            using (var resp = (FtpWebResponse) request.GetResponse())
            {
                Console.WriteLine(resp.StatusCode);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-29 01:49

    Creating an FTP directory might be complicated since you have to check if the destination folder exists or not. You may need to use an FTP library to check and create a directory. You can take a look at this one: http://www.componentpro.com/ftp.net/ and this example: http://www.componentpro.com/doc/ftp/Creating-a-new-directory-Synchronously.htm

    0 讨论(0)
  • 2020-11-29 01:52

    Here is the answer if you want to create nested directories

    There is no clean way to check if a folder exist on the ftp so you have to loop and create all the nested structure one folder at the time

    public static void MakeFTPDir(string ftpAddress, string pathToCreate, string login, string password, byte[] fileContents, string ftpProxy = null)
        {
            FtpWebRequest reqFTP = null;
            Stream ftpStream = null;
    
            string[] subDirs = pathToCreate.Split('/');
    
            string currentDir = string.Format("ftp://{0}", ftpAddress);
    
            foreach (string subDir in subDirs)
            {
                try
                {
                    currentDir = currentDir + "/" + subDir;
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(currentDir);
                    reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(login, password);
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    ftpStream = response.GetResponseStream();
                    ftpStream.Close();
                    response.Close();
                }
                catch (Exception ex)
                {
                    //directory already exist I know that is weak but there is no way to check if a folder exist on ftp...
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题