Click here to Skip to main content
16,012,468 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I want to browse the folders available in an ftp account,
I managed to browse the files but only the files on the root,
I want to be able to browse the folders
I am trying to make a upload app where you can CHOOSE a folder from the ftp to upload the file there,


private void getFileList()
        {
            List<string> files = new List<string>();

            try
            {
                //Optional
                this.Text = "Connecting...";
                Application.DoEvents();

                //Create FTP request
                FtpWebRequest request = FtpWebRequest.Create(ftpAddress) as FtpWebRequest;

                request.Method = WebRequestMethods.Ftp.ListDirectory;
                request.Credentials = new NetworkCredential(ftpusername, ftppassword);
                request.UsePassive = true;
                request.UseBinary = true;
                request.KeepAlive = false;

                //Read the server's response
                this.Text = "Retrieving List...";
                Application.DoEvents();

                FtpWebResponse response = request.GetResponse() as FtpWebResponse;
                Stream responseStream = response.GetResponseStream();
                StreamReader reader = new StreamReader(responseStream);

                while (!reader.EndOfStream)
                {
                    Application.DoEvents();
                  
                    files.Add(reader.ReadLine());
                }

                //Clean-up
                reader.Close();
                responseStream.Close(); //redundant
                response.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("There was an error connecting to the FTP Server");
            }



            

            //If the list was successfully received, display it to the user
            //through a dialog
            if (files.Count != 0)
            {
                
                listDialogForm dialog = new listDialogForm(files);
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    //Update the File Name field
                   // textBox2.Text = dialog.ChosenFile;
                }
            }
        }



and this is for browsing:


C#
public partial class listDialogForm : Form
    {

        private List<string> fileList = new List<string>();
        private string chosenFile;
        private bool p;



        public listDialogForm(List<string> files)
        {
            InitializeComponent();
            fileList = files; //the list of files to display

        }

        public listDialogForm(bool p)
        {
            // TODO: Complete member initialization
            this.p = p;
        }
        public string ChosenFile
        {
            get { return chosenFile; }
        }

        private void listDialogForm_Load(object sender, EventArgs e)
        {
            //Setup the dialog buttons
            btnOK.DialogResult = DialogResult.OK;
            btnCancel.DialogResult = DialogResult.Cancel;

            //Populate the list
            foreach (string file in fileList)
            {


                if(!file.ToString().Contains("."))
                listBox1.Items.Add(file);

            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //Save which file was selected
            if (listBox1.SelectedIndex != -1)
                chosenFile = listBox1.SelectedItem.ToString();
        }
    }
}


if the file contains any "dot" dont show it,
so I can see only the folders
C#
if(!file.ToString().Contains("."))
               listBox1.Items.Add(file);



but I want to enter them and save their destination,,,
Posted

1 solution

What kind of short-sightedness makes you assume that folders cannot contain a period, or as you would say "a dot"?

Regards,

—MRB
 
Share this answer
 
Comments
fjdiewornncalwe 16-Sep-11 15:20pm    
Agreed, but I'm sure at some point in time when you were first starting out you probably made an assumption error or two as well. I know I did. (+5 BTW)

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