Take the 2-minute tour ×

I was trying to upload a file to check the directory exist if not then create one then upload.

Following code suppose to check the exist if not create one,but i have weird error after it says created one, i couldn't access the folder and throws 550 CWD failed. direcotry not found (filezilla).

The code below fixed my problem

            FileInfo fileInf = new FileInfo(filename);
            //create the directory
            FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(path+fileInf.Name));
            reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
            reqFTP.Credentials = new NetworkCredential("TestUpload", "SE");
            reqFTP.KeepAlive = false;
            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

            reqFTP.UsePassive = true;
            reqFTP.UseBinary = true;
            reqFTP.Timeout = -1;
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream ftpStream = response.GetResponseStream();
            reqFTP.ContentLength = fileInf.Length;

            // The buffer size is set to 2kb
            int buffLength = 4096;
            byte[] buff = new byte[buffLength];
            int contentLen;

            // to be uploaded
            FileStream fs = fileInf.OpenRead();

                // Stream to which the file to be upload is written
                Stream strm = reqFTP.GetRequestStream();

                // Read from the file stream 2kb at a time
                contentLen = fs.Read(buff, 0, buffLength);

                // Till Stream content ends
                while (contentLen != 0)
                    // Write Content from the file stream to the FTP Upload
                    // Stream
                    strm.Write(buff, 0, contentLen);
                    contentLen = fs.Read(buff, 0, buffLength);

                // Close the file stream and the Request Stream
            catch (Exception ex)

        catch (WebException ex)
            FtpWebResponse response = (FtpWebResponse)ex.Response;
            if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)



path look like : "",i suppose to drop my file here in "201201" folder.

Am using Filezilla ftp server for my testing.

Any suggestion or help please.

Actually this the line i want all in my code "reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory; "

Thanks in Advance

share|improve this question
possible duplicate of C# How to check if an FTP Directory Exists –  M.Babcock Feb 15 '12 at 0:28
Thanks M.Babcock,already start using that code and works perfectly.am not sure what i missed in my previous code. –  Usher Feb 15 '12 at 0:44
Are you still looking for an answer or is it worth reposting the answer from that question as an answer to get this one closed? –  M.Babcock Feb 15 '12 at 1:26
@M.Babcock,nope that was my answer,so am not looking for one.pardon me if that's not appropriate. –  Usher Feb 15 '12 at 2:43
add comment

Your Answer



Browse other questions tagged or ask your own question.