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);
}
}