Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I was using Make directory to create a folders and subfolder in my ftp (using filezilla) works fine,but when i try to do in my test server (IIS FTP) doesn't work ,throws 550,file not found or no access.so just a quick way to change the code to create subdirctory in my ftp server works fine but i know its a kinda sh*tty way to do like that.

Some one please advice the second thought to approach.

C#
string path = "ftp://1.1.1.1/media/times/" + Name + "/test/fileName";
string[] pathsplit = path.ToString().Split('/');
string Firstpath = pathsplit[0] + "/" + pathsplit[1] + "/" + pathsplit[2] + "/" + pathsplit[3];
string SecondPath = Firstpath + "/" + pathsplit[4];
string ThirdPath = SecondPath + "/" + pathsplit[5];
int count = pathsplit.Count();


try
{
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(path);
    request.Method = WebRequestMethods.Ftp.ListDirectory;
    request.Credentials = new NetworkCredential("sh", "se");
    using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
    {
        // Okay.  
        upload();
    }
}
catch (WebException ex)
{
    try
    {
       //create the first directory if its not there
       //If already there call the catch
        FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(Firstpath));
        createdir.Method = WebRequestMethods.Ftp.MakeDirectory;
        createdir.Credentials = new NetworkCredential("sh", "se");
        createdir.UsePassive = true;
        createdir.UseBinary = true;
        createdir.KeepAlive = false;
        FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse();
        Stream ftpStream1 = response1.GetResponseStream();
        ftpStream1.Close();
        response1.Close();
    }
    catch (Exception e)
    {
        try
        {
           //create the second directory if its not there
           //If already there call the catch
            FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(SecondPath));
            createdir.Method = WebRequestMethods.Ftp.MakeDirectory;
            createdir.Credentials = new NetworkCredential("sh", "se");
            createdir.UsePassive = true;
            createdir.UseBinary = true;
            createdir.KeepAlive = false;
            FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse();
            Stream ftpStream1 = response1.GetResponseStream();
            ftpStream1.Close();
            response1.Close();
        }
        catch (Exception el)
        {
            try
            {
					//create the third directory if its not there
       //If already there call the catch
                FtpWebRequest createdir = (FtpWebRequest)FtpWebRequest.Create(new Uri(ThirdPath));
                createdir.Method = WebRequestMethods.Ftp.MakeDirectory;
                createdir.Credentials = new NetworkCredential("sh", "se");
                createdir.UsePassive = true;
                createdir.UseBinary = true;
                createdir.KeepAlive = false;
                FtpWebResponse response1 = (FtpWebResponse)createdir.GetResponse();
                Stream ftpStream1 = response1.GetResponseStream();
                ftpStream1.Close();
                response1.Close();
            }
            catch
            { }
        
        }
    }

    if (ex.Response != null)
    {
        FtpWebResponse response = (FtpWebResponse)ex.Response;
        if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
        {
            // Directory not found.  
        }
    }
}
Posted
Updated 9-Mar-12 23:43pm
v2

1 solution

Hi,

do you checked if path is correct after splitting?
Anyway, you have too much code in my opinion, you could write a simple method for FtpWebRequest(s) where you can check if File/Directory Exists.

With Best Regards
 
Share this answer
 

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