简体   繁体   中英

How to select files within a directory instead of the entire directory?

I am able to click on my browse button to select multiple files, but I would like to go inside of those files and select what I want. Now, it just allows me to pick the entire folder of files.

namespace ListPrac
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog FBD = new FolderBrowserDialog();

            if (FBD.ShowDialog() == DialogResult.OK)
            {
                listBox1.Items.Clear();
                string[] files = Directory.GetFiles(FBD.SelectedPath);
                string[] dirs = Directory.GetDirectories(FBD.SelectedPath);

                foreach(string file in files)
                {
                    listBox1.Items.Add(file);
                }

                foreach (string dir in dirs)
                {
                    listBox1.Items.Add(dir);
                }
            }

        }
    }
}
        using (OpenFileDialog openFileDialog = new OpenFileDialog())
        {
            openFileDialog.InitialDirectory = @"C:\Folder\";
            openFileDialog.FilterIndex = 2;
            openFileDialog.RestoreDirectory = true;
            openFileDialog1.Multiselect = true;
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                    foreach (string file in openFileDialog1.FileNames) 
                    {
                            listBox1.Items.Add(file);
                            //Do whatever it is you want with them
                    }
            }
        }

This will allow you to navigate folders. OpenFileDialog.Filename will give you the path location of the selected file so you can enumerate through the file using that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM