locked
Choosing FTP path with Drop Down Control in C# ASP.NET RRS feed

  • Question

  • User-265772647 posted

     Hi ,

    In my application im trying to check dropdown value with folder name in ftp server.  But pathd.Contains (maincategory.SelectedItem.Text) always it enters into the loop. If I want to create the directory, it should go to else part but it goes to if part. Please any suggestion instead of path.contains(); I tried with directory exists it is also not wotking.

     DropDownList clientid = (DropDownList)FormView1.FindControl("ddlClient");
            DropDownList maincategory = (DropDownList)FormView1.FindControl("ddlmaincategory");
            TextBox CategoryName = (TextBox)FormView1.FindControl("txtCategoryName");
     string pathd = @"ftp://path" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text+ "/";
    
     if (pathd.Contains (maincategory.SelectedItem.Text ))
            {
                
                string path33 = @"ftp://path" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/" + CategoryName.Text + "/";
    
                FtpWebRequest request33 = (FtpWebRequest)FtpWebRequest.Create(path33);
                request33.Credentials = new NetworkCredential("username", "Password");
                request33.UsePassive = true;
                request33.UseBinary = true;
                request33.KeepAlive = false;
                request33.Method = WebRequestMethods.Ftp.MakeDirectory;
                FtpWebResponse response22 = (FtpWebResponse)request33.GetResponse();
    }
    
    else  //if new directory 
    {
      string path1 = @"ftppath" + clientid.SelectedItem.Text + "/";
                FtpWebRequest request1 = (FtpWebRequest)FtpWebRequest.Create(path1);
                request1.Credentials = new NetworkCredential("username", "password");
                request1.UsePassive = true;
                request1.UseBinary = true;
                request1.KeepAlive = false;
                request1.Method = WebRequestMethods.Ftp.MakeDirectory;
                //FtpWebResponse response = (FtpWebResponse)request1.GetResponse();
               
                if (path1.Contains(maincategory.SelectedItem.Text))
                {
                    // Label1.Text = "alreadey exists";
    
                    string path33 = @"ftppath" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/" + CategoryName.Text + "/";
    
                    FtpWebRequest request33 = (FtpWebRequest)FtpWebRequest.Create(path33);
                    request33.Credentials = new NetworkCredential("useername", "password");
                    request33.UsePassive = true;
                    request33.UseBinary = true;
                    request33.KeepAlive = false;
                    request33.Method = WebRequestMethods.Ftp.MakeDirectory;
                    FtpWebResponse response24 = (FtpWebResponse)request33.GetResponse();
                }
    
    
                string path2 = @"ftppath" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/" + CategoryName.Text + "/";
    
    
                FtpWebRequest request2 = (FtpWebRequest)FtpWebRequest.Create(path2);
                request2.Credentials = new NetworkCredential("username", "password");
                request2.UsePassive = true;
                request2.UseBinary = true;
                request2.KeepAlive = false;
                request2.Method = WebRequestMethods.Ftp.MakeDirectory;
                //FtpWebResponse response1 = (FtpWebResponse)request2.GetResponse();
    
                if (path2.Contains(CategoryName.Text))
                {
                    Label1.Text = "existed";
                }
                else
                {
                    string path3 = @"payh" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/" + CategoryName.Text + "/";
    
                    FtpWebRequest request3 = (FtpWebRequest)FtpWebRequest.Create(path3);
                    request3.Credentials = new NetworkCredential("username", "password");
                    request3.UsePassive = true;
                    request3.UseBinary = true;
                    request3.KeepAlive = false;
                    request3.Method = WebRequestMethods.Ftp.MakeDirectory;
                    FtpWebResponse response2 = (FtpWebResponse)request3.GetResponse();
    
    
                }
    }

    Wednesday, June 18, 2014 11:14 AM

Answers

  • User-718146471 posted

    emaak

    string pathd = @"ftp://" + host + "/Reports/Clients/" + Clientid.SelectedItem.Text + "/" + 
    maincategory.SelectedItem.Text + "/"; if (!path13.Contains(clientid.SelectedValue.ToString())) //it create newfolder
    // but for next time when if I want to create new maincategory or subfolder this loop
    // is again entering but I want to check if not existed other wise exits this loop .


    The problem is you have your checking path created as path12 but you are checking for some variable called path13 which is not the same.  I am thinking you need to go through your variables and ensure that you have named them correctly or you will keep having this problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 10, 2014 10:02 AM

All replies

  • User-718146471 posted

    My best suggestion to you is to set a breakpoint at the if and then debug it to see what the values you are getting are.  Then come back with that information and we'll try to help you. In particular, watch out for the values of pathd.  And of course, watch out for how your FtpWebRequest reponds.  Also, something with the code looks a little off from what I can see.

    Wednesday, June 18, 2014 12:10 PM
  • User-1360095595 posted

    You have maincategory.SelectedItem.Text in the definition of pathd. So your if condition will ALWAYS be true. 

    Edit: changed is to in. 

    Wednesday, June 18, 2014 12:32 PM
  • User-265772647 posted

    When i debug code  if maincategory is not existed on ftp server   it goes to if loop and  it gives error 550 file not found.Is there  any solution that i can check with in if loop if main category existed create subcategory otherwise create main category and subcategory

     if (pathd.Contains (maincategory.SelectedItem.Text ))
            {
    ;..
     FtpWebResponse response22 = (FtpWebResponse)request33.GetResponse();//550 error
    
    }

    else creates client folder and main category and  subcategory .
    .

    Wednesday, June 18, 2014 12:36 PM
  • User-718146471 posted

    You have maincategory.SelectedItem.Text is the definition of pathd. So your if condition will ALWAYS be true. 

    Until you fix that problem, you are not going to get very far. :-)

     

    Wednesday, June 18, 2014 12:40 PM
  • User-718146471 posted

    if (pathd.Contains (maincategory.SelectedItem.Text ))
    

    This right here will always be true.  I would think do it this way:

    if ((pathd.Contains(maincategory.SelectedItem.Text)) == "33")
    

    Wednesday, June 18, 2014 12:41 PM
  • User-265772647 posted

    Is there any way that i can  check with ftp server maincategory folder with pathd.contains (maincategory) in code.

    Wednesday, June 18, 2014 12:46 PM
  • User-718146471 posted

    Well, something I see that is missing is you are not authenticating against the FTP server.  If you do not authenticate against it, it will not permit you access to read, write, etc.

    Wednesday, June 18, 2014 12:48 PM
  • User-265772647 posted

    When i click on folder permission in ftp its not giving any options read ,write.

    I tried below code which create  main category and subcategory if not exists if maincategory exists then its shows error

    if (path.Contains(ddlClient.SelectedItem.Text))
             {
                 
                 string path1 = @"ftp://path/Reports/Clients/" + clientid.SelectedItem.Text + "/";
               
    
                 if (path1.Contains(maincategory.SelectedItem.Text))
                 {
                     Label1.Text = "alreadey exists";
                 }
    
                 else
                 {
    
                     string path2 = @"ftp://path/Reports/Clients/" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text;
                     FtpWebRequest request2 = (FtpWebRequest)FtpWebRequest.Create(path2);
                     request2.Credentials = new NetworkCredential("user", "pass");
                     request2.UsePassive = true;
                     request2.UseBinary = true;
                     request2.KeepAlive = false;
                     request2.Method = WebRequestMethods.Ftp.MakeDirectory;
                     FtpWebResponse response1 = (FtpWebResponse)request2.GetResponse();
    
                     if (path2.Contains(CategoryName.Text))
                     {
                         Label1.Text = "existed";
                     }
                     else
                     {
                         string path3 = @"ftp://path/Reports/Clients/" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/" + CategoryName.Text + "/";
    
                         FtpWebRequest request3 = (FtpWebRequest)FtpWebRequest.Create(path3);
                         request3.Credentials = new NetworkCredential("use", "pass");
                         request3.UsePassive = true;
                         request3.UseBinary = true;
                         request3.KeepAlive = false;
                         request3.Method = WebRequestMethods.Ftp.MakeDirectory;
                         FtpWebResponse response2 = (FtpWebResponse)request3.GetResponse();
    
    
                     }
    
                 }
    
             }
            else
            {
    
    
    
                string path1 = @"ftp://path/Reports/Clients/" + clientid.SelectedItem.Text + "/";
                      
              
                if (path1.Contains(maincategory.SelectedItem.Text)) //second time if maincategory exists it should go to this loop but I don't know how to solve this problem  
                {
                    Label1.Text = "alreadey exists";
                }
    
                       else
                {
                   
                    string path2 = @"ftp://path/Reports/Clients/" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text;
      FtpWebRequest request2 = (FtpWebRequest)FtpWebRequest.Create(path2);
                    request2.Credentials = new NetworkCredential("user", "pass");
                    request2.UsePassive = true;
                    request2.UseBinary = true;
                    request2.KeepAlive = false;
                    request2.Method = WebRequestMethods.Ftp.MakeDirectory;
                    FtpWebResponse response1 = (FtpWebResponse)request2.GetResponse();
    
                    if (path2.Contains(CategoryName.Text))
                    {
                        Label1.Text = "existed";
                    }
                    else
                    {
                        string path3 = @"ftp://path/Portal/Reports/Clients/" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/" + CategoryName.Text + "/";
    
                        FtpWebRequest request3 = (FtpWebRequest)FtpWebRequest.Create(path3);
                        request3.Credentials = new NetworkCredential("user", "pass");
                        request3.UsePassive = true;
                        request3.UseBinary = true;
                        request3.KeepAlive = false;
                        request3.Method = WebRequestMethods.Ftp.MakeDirectory;
                        FtpWebResponse response2 = (FtpWebResponse)request3.GetResponse();
    
    
                    }
    
                }
            }
    

    Wednesday, June 18, 2014 3:55 PM
  • User-718146471 posted

    emaak, we need you to be a little more specific when you say error, specifically what is the error the debugger shows you?  I would the directory creation block of code inside a try/catch block like this:

    else
    {
                 try
                 {
                     string path2 = @"ftp://path/Reports/Clients/" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text;
                     FtpWebRequest request2 = (FtpWebRequest)FtpWebRequest.Create(path2);
                     request2.Credentials = new NetworkCredential("user", "pass");
                     request2.UsePassive = true;
                     request2.UseBinary = true;
                     request2.KeepAlive = false;
                     request2.Method = WebRequestMethods.Ftp.MakeDirectory;
                     FtpWebResponse response1 = (FtpWebResponse)request2.GetResponse();
    
                 }
                 catch (Exception ex)
                 {
                      Response.Write(ex.ToString());
                 }

    This way, when it encounters an error, it will actually send it to the console; very helpful when attempting to debug code.

    Thursday, June 19, 2014 7:03 AM
  • User-265772647 posted

    tried with try catch

    It shows error : The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

    string path2 = @"ftp://path/Reports/Clients/" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text;
                     FtpWebRequest request2 = (FtpWebRequest)FtpWebRequest.Create(path2);
                     request2.Credentials = new NetworkCredential("user", "pass");
                     request2.UsePassive = true;
                     request2.UseBinary = true;
                     request2.KeepAlive = false;
                     request2.Method = WebRequestMethods.Ftp.MakeDirectory;
                     FtpWebResponse response1 = (FtpWebResponse)request2.GetResponse(); //error
    

    I tried with getting list of  directories from ftp and compare with dropdownlist values  ,First time it checks with  dropdown list option with ftp folder  if not exists  creates folder   second time  if  folder exists again  it goes to if loop and shows the  same error  550. 

    Thursday, June 19, 2014 9:11 AM
  • User-718146471 posted

    Ok, first test you can do to check your access is log on as the user/pass using an FTP client like FileZilla.  Once you log on, try to create a file and a folder.  IF you cannot, the problem is on the FTP side of the house.  If you are able to create files/folders, we'll look at your code more closely.

    Thursday, June 19, 2014 9:42 AM
  • User-718146471 posted

    Aslo, is this FTP site Windows or Linux?  If it is linux, your user accounts will require permission level 664.  If windows, the user account will need read/write/list.

    Thursday, June 19, 2014 9:43 AM
  • User-265772647 posted

    Im able to create folder in ftp site  its in windows .

    through code also  in else part if clientfolder not existed in ftp server  then  it is able to create clientfolder/mainfolder/subfolder  . in if part  it checks   for dropdownclient  if particular logins then it create sub category .Its not  checking maincategory exists or not .

    structure

    1.user logins with id, password   att ,att

    2.  open in form  with  clientdropdown has options  att,attge,verizion,..  ,maincategory dropwn (business,payment,..) ,subcategory texbtox.  save buttong.

     if user selects att in  client dropdown

    if   (cleintdropdown ==att)  //checks here if user selects  one present in ftp server or  need to create new  client folder  .If it is new folder then goes to else part and create  new  folder /main/sub  

    {

    check (if (maincategory  exists in att) //if exists 

    {

    create subcategory folder

    }   else{  create main catogory  and sub category

    }

    }

    else     //if user  want to create new client folder it goes to else part and creates new client folder.

    {

    }

    Thursday, June 19, 2014 10:09 AM
  • User-718146471 posted

    Ok, now that I have everything I am able to point out where your problem is.  This line:

            ftp://path/Reports/Clients/

    is the problem.  You need to recode it. 

    protected void FormView1_ItemInserted(object sender, System.Web.UI.WebControls.FormViewInsertedEventArgs e)
    
    {
    DropDownList clientid = (DropDownList)FormView1.FindControl("ddlClient");
    DropDownList maincategory = (DropDownList)FormView1.FindControl("ddlmaincategory");
    TextBox CategoryName = (TextBox)FormView1.FindControl("txtCategoryName");
    var uri = HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port;
    string host = uri.ToString();
    string path12 = @"ftp://" + host + "/Reports/Clients/" + clientid.SelectedItem.Text + "/";
    

     

    Tuesday, July 1, 2014 1:30 PM
  • User-265772647 posted

    Below changed code  creates  main category folder  if  not existed and creates subcategory folder also.but    I don't know how to exit from for loop if directory exists.

    protected void FormView1_ItemInserted(object sender, System.Web.UI.WebControls.FormViewInsertedEventArgs e)
        {
            string categoryName;
            DropDownList clientid = (DropDownList)FormView1.FindControl("ddlClient");
            DropDownList maincategory = (DropDownList)FormView1.FindControl("ddlmaincategory");
            TextBox CategoryName = (TextBox)FormView1.FindControl("txtCategoryName");
    
            string path12 = @"ftp://path/Reports/Clients/" + clientid.SelectedItem.Text + "/";
         
    
            FtpWebRequest ftpRequest1 = (FtpWebRequest)WebRequest.Create(path12);
            ftpRequest1.Credentials = new NetworkCredential("user", "pass");
            ftpRequest1.Method = WebRequestMethods.Ftp.ListDirectory;
            ftpRequest1.UsePassive = true;
            ftpRequest1.UseBinary = true;
            ftpRequest1.KeepAlive = false;
            FtpWebResponse response1 = (FtpWebResponse)ftpRequest1.GetResponse();
            StreamReader streamReader1 = new StreamReader(response1.GetResponseStream());
            List<string> directories1 = new List<string>();
    
            string line1 = streamReader1.ReadLine();
    
    
            string path = @"ftp://path/QPortal/Reports/Clients/";
    
    
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(path);
            ftpRequest.Credentials = new NetworkCredential("user", "pass");
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
            ftpRequest.UsePassive = true;
            ftpRequest.UseBinary = true;
            ftpRequest.KeepAlive = false;
            FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
            StreamReader streamReader = new StreamReader(response.GetResponseStream());
    
            List<string> directories = new List<string>();
    
            string line = streamReader.ReadLine();
            while (!string.IsNullOrEmpty(line))
            {
    
                if (clientid.SelectedItem.Text == line) //loop for client folders in ftp server.it gets all client folders
                {
                    while (!string.IsNullOrEmpty(line1))//loop for maincategoryfolders it reads each folder
                    {
                        for (int i = 0; i < line1.Length; i++)
                        {
                            string pathmain = @"ftp://path/Reports/Clients/" + clientid.SelectedItem.Text + "/" + maincategory.SelectedValue.Trim() + "/";
    
    
                            DirectoryExists(pathmain);
                                                 
                       }
                      
    
                        directories1.Add(line1);
                        line1 = streamReader1.ReadLine();
    
    
                        
    
                    }
    
    
                }
    
                else
                {
    
                }
    
    
                directories.Add(line);
                line = streamReader.ReadLine();
            }
    
    
            streamReader.Close();
    
    
            lstCategories.DataBind();
    
    
        }
      








    public bool DirectoryExists(string directory) { bool directoryExists; DropDownList clientid = (DropDownList)FormView1.FindControl("ddlClient"); DropDownList maincategory = (DropDownList)FormView1.FindControl("ddlmaincategory"); TextBox CategoryName = (TextBox)FormView1.FindControl("txtCategoryName"); var request = (FtpWebRequest)WebRequest.Create(directory); request.Method = WebRequestMethods.Ftp.ListDirectory; request.Credentials = new NetworkCredential("user", "pass"); try { using (request.GetResponse()) { directoryExists = true; string path2 = @"ftp://path/Reports/Clients/" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/" + CategoryName.Text + "/"; FtpWebRequest request2 = (FtpWebRequest)FtpWebRequest.Create(path2); request2.Credentials = new NetworkCredential("user", "pass"); request2.UsePassive = true; request2.UseBinary = true; request2.KeepAlive = false; request2.Method = WebRequestMethods.Ftp.MakeDirectory; FtpWebResponse response23 = (FtpWebResponse)request2.GetResponse(); } } catch (WebException) { directoryExists = false; CreateDirectoryOnFTP("ftp://path/Reports/Clients/" + clientid.SelectedItem.Text, "aargen", "Zorro123", maincategory.SelectedItem.Text); } return directoryExists; } static void CreateDirectoryOnFTP(String inFTPServerAndPath, String inUsername, String inPassword, String inNewDirectory) { FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(inFTPServerAndPath + "/" + inNewDirectory); request.Credentials = new NetworkCredential(inUsername, inPassword); request.UsePassive = true; request.UseBinary = true; request.KeepAlive = false; request.Method = WebRequestMethods.Ftp.MakeDirectory; FtpWebResponse makeDirectoryResponse = (FtpWebResponse)request.GetResponse(); }

    Thursday, July 3, 2014 10:52 AM
  • User-718146471 posted

    string path12 = @"ftp://path/Reports/Clients/" + clientid.SelectedItem.Text + "/";
    

    emaak, why are you still using this obsolete code?  You should be using the newer code.  I'm thinking you may want to check for the variable for path as your check point.  Set a breakpoint and step through to see what it is doing.  Watch the values :)

    Thursday, July 3, 2014 11:12 AM
  • User-265772647 posted
      protected void FormView1_ItemInserted(object sender, System.Web.UI.WebControls.FormViewInsertedEventArgs e)
        {
            // string categoryName;
            DropDownList clientid = (DropDownList)FormView1.FindControl("ddlClient");
            DropDownList maincategory = (DropDownList)FormView1.FindControl("ddlmaincategory");
            TextBox CategoryName = (TextBox)FormView1.FindControl("txtCategoryName");
            var uri = "path";
            string host = uri.ToString();
    
    
            string path12 = @"ftp://" + host + "/Reports/Clients/" + clientid.SelectedItem.Text + "/";
    
            string pathd = @"ftp://" + host + "/Reports/Clients/" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/";
    
    
            FtpWebRequest ftpRequest1 = (FtpWebRequest)WebRequest.Create(path12);
            ftpRequest1.Credentials = new NetworkCredential("user", "pass");
            ftpRequest1.Method = WebRequestMethods.Ftp.ListDirectory;
            ftpRequest1.UsePassive = true;
            ftpRequest1.UseBinary = true;
            ftpRequest1.KeepAlive = false;
            FtpWebResponse response1 = (FtpWebResponse)ftpRequest1.GetResponse();
            StreamReader streamReader1 = new StreamReader(response1.GetResponseStream());
            List<string> directories1 = new List<string>();
    
            string line1 = streamReader1.ReadLine();
    
    
            string path22 = @"ftp://" + host + "/Reports/Clients/";
    
    
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(path22);
            ftpRequest.Credentials = new NetworkCredential("user", "pass");
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
            ftpRequest.UsePassive = true;
            ftpRequest.UseBinary = true;
            ftpRequest.KeepAlive = false;
            FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
            StreamReader streamReader = new StreamReader(response.GetResponseStream());
    
            List<string> directories = new List<string>();
    
            string line = streamReader.ReadLine();
            while (!string.IsNullOrEmpty(line))
            {
    
                if (clientid.SelectedItem.Text == line)
                {
    
                    while (!string.IsNullOrEmpty(line1))
                    {
                        //for (int i = 0; i < line1.Length; i++)
                        //{
    
                        if (maincategory.SelectedItem.Text == line1)
                        {
                            string path2 = @"ftp://" + host + "/Reports/Clients/" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/" + CategoryName.Text + "/";
    
                            FtpWebRequest request2 = (FtpWebRequest)FtpWebRequest.Create(path2);
                            request2.Credentials = new NetworkCredential("user", "pass");
                            request2.UsePassive = true;
                            request2.UseBinary = true;
                            request2.KeepAlive = false;
                            request2.Method = WebRequestMethods.Ftp.MakeDirectory;
                            FtpWebResponse response23 = (FtpWebResponse)request2.GetResponse();
                            break;
    
                        }
    
                          else
                        {
                        }
                      
                       
                      
                        directories1.Add(line1);
                        line1 = streamReader1.ReadLine();
    
                        if (!DirectoryExists(pathd))
                        {
                            CreateDirectoryOnFTP("ftp://" + host + "/Reports/Clients/" + clientid.SelectedItem.Text, "aargen", "Zorro123", maincategory.SelectedItem.Text);
                            string path2 = @"ftp://" + host + "/Reports/Clients/" + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/" + CategoryName.Text + "/";
    
                            FtpWebRequest request2 = (FtpWebRequest)FtpWebRequest.Create(path2);
                            request2.Credentials = new NetworkCredential("user", "pass");                        request2.UsePassive = true;
                            request2.UseBinary = true;
                            request2.KeepAlive = false;
                            request2.Method = WebRequestMethods.Ftp.MakeDirectory;
                            FtpWebResponse response23 = (FtpWebResponse)request2.GetResponse();
                            break;
                        }
                    }
    
                }
    
                else
                {
                }
    
    
    
    
                directories.Add(line);
                line = streamReader.ReadLine();
            }
    
    
            streamReader.Close();
    
    
            lstCategories.DataBind();
    
    
        }
    

    i check with newer code and break points for the firsttime  it creates mainfolder and sub folder in ftp server.when i  try to create another subfolder within same main category  it creates subfolder but it shows  error  550 file not found. 

    Thursday, July 3, 2014 1:58 PM
  • User-718146471 posted

     

     protected void FormView1_ItemInserted(object sender, System.Web.UI.WebControls.FormViewInsertedEventArgs e)
        {
            // string categoryName;
            DropDownList clientid = (DropDownList)FormView1.FindControl("ddlClient");
            DropDownList maincategory = (DropDownList)FormView1.FindControl("ddlmaincategory");
            TextBox CategoryName = (TextBox)FormView1.FindControl("txtCategoryName");
            var uri = "path"; // this line is bothering me, you are setting uri to the word "path", that is not going to work
    string host = uri.ToString();

    I wanted to point out that line will give you grief if you actually specify the word path as the path.  Did you check out what I sent you?

    Thursday, July 3, 2014 2:13 PM
  • User-265772647 posted
    var uri = "path"; //   instead of original  ftp path (ftp://aargents.com/aargend/QPortal/Reports/Client)  im mentioning  "path" 
     string host = uri.ToString();

    im sorry for this confusion.

      var uri = HttpContext.Current.Request.Url.Host + ":" + HttpContext.Current.Request.Url.Port;
            string host = uri.ToString();
    
    instead of above code im using below one  
     var uri = "aargents.com/aargend/QPortal";
            string host = uri.ToString();

    Thursday, July 3, 2014 2:30 PM
  • User-718146471 posted

    instead of above code im using below one  
     var uri = "aargents.com/aargend/QPortal";
            string host = uri.ToString();

    You should not need to hard-code the path, hard-coding is always a really bad idea for many reasons.  What about various testing methods?  By hard coding you will not find the right path locally.

    Monday, July 7, 2014 11:55 AM
  • User-265772647 posted

    Hi,

    Now it working   with createing  mainfolder and subcategory folder .But I don't know how to check with clientfolder

    Structure:--- 

    Clients folder  (ATT,GE,...)  //.now I want to check in client folder evolution folder existed or not

    MainCategory Folder  (Example ATT --businessoperations ,ex1,ex2)//ex1,ex2,bo are maincategory folders

    subcategory folder(ATT/ex1/test)//test is subctegory folder

    protected void FormView1_ItemInserted(object sender, System.Web.UI.WebControls.FormViewInsertedEventArgs e)
        {
            // string categoryName;
            DropDownList clientid = (DropDownList)FormView1.FindControl("ddlClient");
            DropDownList maincategory = (DropDownList)FormView1.FindControl("ddlmaincategory");
            TextBox CategoryName = (TextBox)FormView1.FindControl("txtCategoryName");
            var uri = "aargent1.com/aarg/QPortal";
            string host = uri.ToString();
    
    
            string path12 = @"ftp://" + host + "/Reports/Clients/" + 
    
    clientid.SelectedItem.Text + "/";
    
            string pathd = @"ftp://" + host + "/Reports/Clients/" + 
    
    clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/";
    
    
    // here if I want to check if client folder contains dropdown selected
    
     folder or not 
    
    
     if (!path13.Contains(clientid.SelectedValue.
    
    ToString())) //it create newfolder  but for next
    
     time when if I want to create new maincategory or
    
     subfolder  this loop  is  again entering but iwant 
    
    to  check if not existed other wise exits this loop .
            {
                string path2 = @"ftp://" + host +
    
     "/Reports/Clients/" + clientid.SelectedItem.Text ;
    
                FtpWebRequest request2 =
    
     (FtpWebRequest)FtpWebRequest.Create(path2);
                request2.Credentials = 
    
    new NetworkCredential("user", "pass");
                request2.UsePassive = true;
                request2.UseBinary = true;
                request2.KeepAlive = false;
                request2.Method = 
    
    WebRequestMethods.Ftp.MakeDirectory;
                FtpWebResponse response23 =
    
     (FtpWebResponse)request2.GetResponse();
    
            }
    
    
    
    
    
    
            FtpWebRequest ftpRequest1 = (FtpWebRequest)WebRequest.Create(path12);
            ftpRequest1.Credentials = new NetworkCredential("user", "pass");
            ftpRequest1.Method = WebRequestMethods.Ftp.ListDirectory;
            ftpRequest1.UsePassive = true;
            ftpRequest1.UseBinary = true;
            ftpRequest1.KeepAlive = false;
            FtpWebResponse response1 = (FtpWebResponse)ftpRequest1.GetResponse();
            StreamReader streamReader1 = 
    
    new StreamReader(response1.GetResponseStream());
            List<string> directories1 = new List<string>();
    
            string line1 = streamReader1.ReadLine();
    
    
            string path = @"ftp://" + host + "/Reports/Clients/";
    
    
            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(path);
            ftpRequest.Credentials = new NetworkCredential("user", "pass");
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
            ftpRequest.UsePassive = true;
            ftpRequest.UseBinary = true;
            ftpRequest.KeepAlive = false;
            FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();
            StreamReader streamReader =
    
     new StreamReader(response.GetResponseStream());
    
            List<string> directories = new List<string>();
    
            string line = streamReader.ReadLine();
            while (!string.IsNullOrEmpty(line))
            {
    
                if (clientid.SelectedItem.Text == line)
                {
    
                    if (!DirectoryExists(pathd))
                    {
                        CreateDirectoryOnFTP("ftp://" + host + "/Reports/Clients/"
    
     + clientid.SelectedItem.Text, "user", "pass", maincategory.SelectedItem.Text);
                        string path2 = @"ftp://" + host + "/Reports/Clients/" 
    
    + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/"
    
     + CategoryName.Text + "/";
    
                        FtpWebRequest request2 = (FtpWebRequest)
    
    FtpWebRequest.Create(path2);
                        request2.Credentials = new NetworkCredential("user", "pass");
                        request2.UsePassive = true;
                        request2.UseBinary = true;
                        request2.KeepAlive = false;
                        request2.Method = WebRequestMethods.Ftp.MakeDirectory;
                        FtpWebResponse response23 = (FtpWebResponse)request2.
    
    GetResponse();
                        break;
                    }
                    while (!string.IsNullOrEmpty(line1))
                    {
                        //for (int i = 0; i < line1.Length; i++)
                        //{
    
                        if (maincategory.SelectedItem.Text == line1)
                        {
                            string path2 = @"ftp://" + host + "/Reports/Clients/"
    
      + clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/" + 
    
    CategoryName.Text;
    
                            FtpWebRequest request2 = 
    
    (FtpWebRequest)FtpWebRequest.Create(path2);
                            request2.Credentials =
    
     new NetworkCredential("user", "pass");
                            request2.UsePassive = true;
                            request2.UseBinary = true;
                            request2.KeepAlive = false;
                            request2.Method = WebRequestMethods.Ftp.MakeDirectory;
                            //FtpWebResponse response23 = 
    
    (FtpWebResponse)request2.GetResponse();
                            break;
    
                        }
                       
                        else
                        {
    
    
                        }
    
                        directories1.Add(line1);
                        line1 = streamReader1.ReadLine();
    
    
    
    
    
                    }
    
                }
    
                else
                {
    
                }
    
    
    
    
                directories.Add(line);
                line = streamReader.ReadLine();
            }
    
    
            streamReader.Close();
    
    
            lstCategories.DataBind();
    
    
        }
      
    
        public bool DirectoryExists(string directory)
        {
            bool directoryExists;
            DropDownList clientid = 
    
    (DropDownList)FormView1.FindControl("ddlClient");
            DropDownList maincategory = 
    
    (DropDownList)FormView1.FindControl("ddlmaincategory");
            TextBox CategoryName = 
    
    (TextBox)FormView1.FindControl("txtCategoryName");
    
            var request = (FtpWebRequest)WebRequest.Create(directory);
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            request.Credentials = new NetworkCredential("user", "pass");
    
            try
            {
                using (request.GetResponse())
                {
                    directoryExists = true;
                    string path2 = 
    
    @"ftp://aargents.com/aar/QPortal/Reports/Clients/" +
    
     clientid.SelectedItem.Text + "/" + maincategory.SelectedItem.Text + "/" + 
    
    CategoryName.Text + "/";
    
                    FtpWebRequest request2 = 
    
    (FtpWebRequest)FtpWebRequest.Create(path2);
                    request2.Credentials = new NetworkCredential("user", "pass");
                    request2.UsePassive = true;
                    request2.UseBinary = true;
                    request2.KeepAlive = false;
                    request2.Method = WebRequestMethods.Ftp.MakeDirectory;
                    FtpWebResponse response23 = 
    
    (FtpWebResponse)request2.GetResponse();
    
                }
            }
            catch (WebException)
            {
                directoryExists = false;
    
              
            }
    
            return directoryExists;
        }
    
    static void CreateDirectoryOnFTP(String inFTPServerAndPath, 
    
    String inUsername, String inPassword, String inNewDirectory)
        {
            // Step 1 - Open a request using the full URI, 
    
    ftp://ftp.server.tld/path/file.ext
            FtpWebRequest request = (FtpWebRequest)FtpWebRequest.
    
    Create(inFTPServerAndPath + "/" + inNewDirectory);
    
            // Step 2 - Configure the connection request
            request.Credentials = new NetworkCredential(inUsername, inPassword);
            request.UsePassive = true;
            request.UseBinary = true;
            request.KeepAlive = false;
    
            request.Method = WebRequestMethods.Ftp.MakeDirectory;
    
            // Step 3 - Call GetResponse() method to actually attempt to create
    
     the directory
            FtpWebResponse makeDirectoryResponse = 
    
    (FtpWebResponse)request.GetResponse();
        }
       
    
    
    
    
    
    


    Monday, July 7, 2014 12:21 PM
  • User-718146471 posted

    emaak

    string pathd = @"ftp://" + host + "/Reports/Clients/" + Clientid.SelectedItem.Text + "/" + 
    maincategory.SelectedItem.Text + "/"; if (!path13.Contains(clientid.SelectedValue.ToString())) //it create newfolder
    // but for next time when if I want to create new maincategory or subfolder this loop
    // is again entering but I want to check if not existed other wise exits this loop .


    The problem is you have your checking path created as path12 but you are checking for some variable called path13 which is not the same.  I am thinking you need to go through your variables and ensure that you have named them correctly or you will keep having this problem.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 10, 2014 10:02 AM
  • User-265772647 posted

    Thank you bbcomponent1.Finally it is  working rt now.I followed your tips and changed code  step by step debugging ,thank you for your time.

    Thursday, July 10, 2014 4:52 PM