How can I upload folder, subfolders and everything that these folders contain to my FTP server with C #?

1

How can I upload my folders to my FTP server and thus there are subfolders that are also uploaded along with all the files they have inside.
I have the following code and this only uploads the name of the folder and the files inside. If there is a folder inside or more, they are not uploaded.

 protected static void UploadFile(string FilePath, string RemotePath, string Login, string Password, string fileName)
    {
        try
        {
            using (FileStream fs = File.Open(FilePath, FileMode.OpenOrCreate, FileAccess.Read))
            {
                string url = RemotePath + "/Backup-PC-Directoy/" + fileName;
                FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(url);
                ftp.Credentials = new NetworkCredential(Login, Password);
                ftp.KeepAlive = false;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;
                ftp.UseBinary = true;
                ftp.ContentLength = fs.Length;
                ftp.Proxy = null;
                byte[] buff = new byte[fs.Length];
                fs.Read(buff, 0, buff.Length);
                fs.Close();
                Stream ftpstream = ftp.GetRequestStream();
                ftpstream.Write(buff, 0, buff.Length);
                ftpstream.Close();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

    }
    
asked by Manny 13.12.2018 в 17:44
source

2 answers

1

With a recursive method you can do it, obtaining all the names of the files and all the sub-directories of the current directory to save them in Array , then iterate for each sub-directory and perform the same procedure.

protected static void UploadFile(string FilePath, string RemotePath, string Login, string Password)
{
    try
    {
        string[] Files = Directory.GetFiles(FilePath, "*.*");
        string[] Paths = Directory.GetDirectories(FilePath);
        foreach (string file in Files)
        {
            using (FileStream fs = File.Open(FilePath, FileMode.OpenOrCreate, FileAccess.Read))
            {
                FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(RemotePath + Path.GetFileName(file));
                ftp.Credentials = new NetworkCredential(Login, Password);
                ftp.KeepAlive = false;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;
                ftp.UseBinary = true;
                ftp.ContentLength = fs.Length;
                ftp.Proxy = null;
                byte[] buff = new byte[fs.Length];
                fs.Read(buff, 0, buff.Length);
                fs.Close();
                Stream ftpstream = ftp.GetRequestStream();
                ftpstream.Write(buff, 0, buff.Length);
                ftpstream.Close();
            }
        }
        foreach (string path in Paths)
        {
            UploadFile(path, RemotePath + "/" + Path.GetFileName(path), Login, Password);
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
    
answered by 13.12.2018 в 18:56
0

Option 1:

  • You can send all your content (folders, sub-folders and others) in a .zip file, and unzip it to the server.

Option 2:

  • Create a recursive method to which you pass the URL of the folder to which to connect and download the items contained in the folder there.
answered by 13.12.2018 в 18:15