Click here to Skip to main content
15,939,781 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

C#
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;


   }
Posted
Comments
Dave Kreskowiak 25-Mar-17 17:43pm    
So what's the question?
Member 12839758 25-Mar-17 18:43pm    
i dont know how to check if folder existes, neither to créate the 3 folders at the same time just the last one
Karthik_Mahalingam 27-Mar-17 0:02am    
Always use  Reply  button, to post Comments/query to the user, so that the user gets notified and responds to your text.
Garth J Lancaster 25-Mar-17 19:44pm    
"i have tryed Ftp.MakeDirectory but these only creates the last folder i need to créate the 3...and dont know how to make these"

yes, thats the way it works, and and answer is, you make the directory levels one at a time - have a look here for thoughts http://stackoverflow.com/questions/2769137/how-to-check-if-an-ftp-directory-exists

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900