hi all,

i had a folderbrowserdialogue. and wen i click on a folder it should get me that folder name, files names in that folder and subfolders and files names in that subfolder to be displayed in listview.
i had tried this and it is displaying files in selected folder.Now i need to get folder name, and subfoldername and files in that subfolder.so can anyone suggest me..

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Data.Odbc;
using System.Data.OleDb;


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

        private void Browse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderBrowserDialog1 = new FolderBrowserDialog();
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                listView1.Items.Clear();
                string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
                foreach (string file in files)
                {
                    string fileName = Path.GetFileNameWithoutExtension(file);
                    ListViewItem item = new ListViewItem(fileName);
                    item.Tag = file;
                    listView1.Items.Add(item);

                }
            }
        }

        
    }
}

Don't know exactly what your intentions are with a Listview.
But beside the fact that Directories has a GetFiles method it also has

string[] folders = Directory.GetDirectories(folderBrowserDialog1.SelectedPath);

ddanbe, but this will only get folders, not files. I think he want to get all files in wanted directory, and in all subdirectories, if Im not mistaken.
This for example will return all the file paths:
string[] filePaths = Directory.GetFiles(@"C:\1", "*.*", SearchOption.AllDirectories);

@Mitja, you are right, but line 31 of his code already gives the OP the files in that directory.
Don't know what the intentions really are, but your suggestion of

string[] filePaths = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*", SearchOption.AllDirectories);

is very good!

And here is an example of how to get all files (only names if you want) from a directory + files in all sub directory (change the file path):

class Program
    {
        static void Main(string[] args)
        {
            List<string> allFiles = GettingFiles(@"C:\1");
        }

        public static List<string> GettingFiles(string path)
        {
            List<string> listFiles = new List<string>();

            //1. get files from the current directory:
            string[] currentFiles = Directory.GetFiles(path, "*.*");
            foreach (string file in currentFiles)
                listFiles.Add(file);

            //2. get files from other sub directories:
            string[] directories = Directory.GetDirectories(path);
            foreach (string dir in directories)
            {
                string[] files = GetFilesFromDirectory(dir);
                listFiles.AddRange(files);
            }

            //for the end, lets get only the names of the files (remove the path):
            for (int i = 0; i < listFiles.Count; i++)
            {
                string fileName = Path.GetFileName(listFiles[i]);
                listFiles[i] = fileName;
            }
            return listFiles;
        }

        private static string[] GetFilesFromDirectory(string path)
        {
            string[] files = Directory.GetFiles(path);
            return files;
        }
    }

btw, thx ddanbe :)
Hope it helps,
Mitja

commented: You could post this as a snippet! +8

thx ddanbe, I will.
You can find it here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.