1

In my program I have a treeview and a folderbrowser and a datagridview. The user uses the folder browser to choose a folder which contains bunch of shows which all have different seasons. My program displays the folders for the shows and the season folders inside them in the treeview and each time the select a folder from treeview I want it to display all the files inside that folder. I am currectly using this code:

public void fileProcessDirectory(string targetDirectory, string Name)
    {

        string[] fileEntries = Directory.GetFiles(targetDirectory);
        foreach (string fileName in fileEntries)
        {
            FileProcessFile(fileName);
        }
        string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach (string subdirectory in subdirectoryEntries)
        {
            fileProcessDirectory(subdirectory, Name);
            break;
        }

    }
    public void FileProcessFile(string path)
    {
        dataGridView.Rows.Add(path, "New");
    }

it shows the files inside the first sub folder that I have. it used to show all the files inside all the folders so I added a break and now it shows the first 3 files and stops there. So I want it to display the files inside the selected subfolder not all the sub folders.

melika
  • 97
  • 2
  • 9
  • 1
    do you have files in sub folders? – mybirthname Dec 02 '16 at 10:34
  • What files is it showing? ".", ".." ? – Markiian Benovskyi Dec 02 '16 at 10:35
  • @mybirthname yes I have a folder for each show and then each show has a folder inside it for the seasons and then the files are all inside the subfolders(seasons) – melika Dec 02 '16 at 10:43
  • @MarkBenovsky it shows the files inside the first sub folder that I have. it used to show all the files inside all the folders so I added a break and now it shows the first 3 files and stops there. – melika Dec 02 '16 at 10:44
  • Check this answer; http://stackoverflow.com/questions/2106877/is-there-a-faster-way-than-this-to-find-all-the-files-in-a-directory-and-all-sub/2107294#2107294 – Kymuweb Dec 02 '16 at 11:02

2 Answers2

1

You can try to modify your function as this:

public void FileProcessDirectory(string targetDirectory, string subfolder)
{
    // this adds files
    foreach (string fileName in Directory.GetFiles(targetDirectory))
    {
        FileProcessFile(fileName);
    }

    // if we pass subfolder as empty then nothing happens
    if(string.IsNullOrEmpty(subfolder)) return;
    // here we find our subfolder and display files for it        
    FileProcessDirectory(Directory.GetDirectories(targetDirectory).Where(d => d == targetDirectory + "\\" + subfolder).ToArray()[0], null);
}

And the ussage example:

FileProcessDirectory(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "Debug");
Markiian Benovskyi
  • 2,137
  • 22
  • 29
  • I get this error : Cannot apply indexing with [] to an expression of type 'IEnumerable' – melika Dec 02 '16 at 11:23
  • now I get another error during run time saying index was outside the bounds of the array – melika Dec 02 '16 at 11:34
  • @melika get directory returns a full path, if your subfolder variable is only name of folder then you should add a target directory path to it, fixed in code above – Markiian Benovskyi Dec 02 '16 at 11:47
  • I tried hat it didn't work still I get the same error and I am getting the full path of the folder. – melika Dec 02 '16 at 13:37
  • @melika This example worked for me, so i dont know what error you have and why, try to debug your code and view the variable values, then check if code works as expected – Markiian Benovskyi Dec 02 '16 at 15:03
0

Please correct my understanding if I'm wrong: user select the folder, then on treeview select the season then they should see in the data grid view all the files inside, correct ?

I implemented in this way

treeView1.NodeMouseDoubleClick += new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);

if user double click on the treenode it shows all the files inside in the data grid:

void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (treeView1.SelectedNode != null)
        {
            dataGridView1.Rows.Clear();
            string[] fileEntries = System.IO.Directory.GetFiles(treeView1.SelectedNode.Text);
            foreach (string fileName in fileEntries)
            {

                dataGridView1.Rows.Add(Path.GetFileName(fileName));
            }
        }
    }

I guess the problem before maybe caused by the dataGrid not clearing the old files. Hope it helps.

farbiondriven
  • 2,450
  • 2
  • 15
  • 31