I want to upload a file to an FTP, however the FTP not always have the directory i want to put my file on and also it may need more tan 1 directory to be created before uploading the file
My Ftp always contains these route:
ftp://server.com/invoice/
however i need to créate theese folders before uploading my file
href="ftp://server.com/Invoices/RFC/RFC2/NOTES">
how ever not always does the ftp has these routes
i need to be able to check if these folders exist and if not créate them, i have tryed Ftp.MakeDirectory but these only creates the last folder i need to créate the 3 of them before uploading a file and dont know how to make these
What I have tried:
this is my code as for now
public void UploadFTP( string userName, string password, string FilePath, string FileName)
{
string hostFTPIP="";
FtpWebRequest ftpWebRequest = null;
Stream ftpStream = null;
int bufferSize = 2048;
string fullpath = FilePath + FileName;
ftpWebRequest = (FtpWebRequest)FtpWebRequest.Create(hostFTPIP);
ftpWebRequest.Credentials = new NetworkCredential(userName, password);
ftpWebRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
ftpWebRequest.UseBinary = true;
ftpWebRequest.UsePassive = false;
ftpWebRequest.KeepAlive = true;
ftpWebRequest.Method = WebRequestMethods.Ftp.UploadFile;
ftpStream = ftpWebRequest.GetRequestStream();
FileStream LocalUserFileStream = new FileStream(fullpath, FileMode.Open, FileAccess.Read);
byte[] byteBuffer = new byte[bufferSize];
int bytesSent = LocalUserFileStream.Read(byteBuffer, 0, bufferSize);
try
{
while (bytesSent != 0)
{
ftpStream.Write(byteBuffer, 0, bytesSent);
bytesSent = LocalUserFileStream.Read(byteBuffer, 0, bufferSize);
}
}
catch (Exception ex)
{
Console.WriteLine("Error in Uploading Files" + ex);
}
LocalUserFileStream.Close();
ftpStream.Close();
ftpWebRequest = null;
}