Sakarin Seppo
Sakarin Seppo

Reputation: 15

Listing all the files in root directory

Thanks for your support! I now have working code to scan all folders, subfolders and files. There is just one problem left to solve:

I do not get the files in the initial root directory, only the subfolders. I also need to call FileInfo for these files.

How could this be resolved without modifying the code too much?

private void ScanFolder(String prefix, String path)
{
    try
    {
        string user = System.IO.File.GetAccessControl(path).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
        DirectoryInfo di = new DirectoryInfo(path);

        foreach (var dir in new DirectoryInfo(path).GetDirectories("*", SearchOption.TopDirectoryOnly))
        {
            listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add(prefix + dir.Name + " (" + dir.Name.Length.ToString() + ")   "); });

            foreach (FileInfo fileInfo in dir.GetFiles())
            {
                listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add(prefix + fileInfo.Name + " (" + fileInfo.Name.Length.ToString() + ")   " + user + "   " + fileInfo.FullName + " (" + fileInfo.FullName.Length.ToString() + ")"); });
            }

            ScanFolder(prefix + "—", dir.FullName);
        }
    }
    catch
    {
        if (!this.IsDisposed)
        {
            listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add("Access Denied to : " + path); });
        }
    }
}

Output:

** The files should be here **
13-9-legacy_vista_win7_64_dd_ccc_whql (37)
Radeon-Software-Adrenalin-18.3.3-MinimalSetup-180319_web (56)
—Bin (3)
——localization (12)
———cs (2)
———da_DK (5)
———de (2)
———el_GR (5)
———es_ES (5)

Upvotes: 0

Views: 933

Answers (1)

IDarkCoder
IDarkCoder

Reputation: 709

So far you're only looking for the directories in the root directory.
You also want to enumerate through the files though:

private void ScanFolder(String prefix, String path)
{
    try
    {
        string user = System.IO.File.GetAccessControl(path).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
        DirectoryInfo di = new DirectoryInfo(path);

        // Enumerate through the files here
        foreach (FileInfo fileInfo in di.GetFiles())
        {
            listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add(prefix + fileInfo.Name + " (" + fileInfo.Name.Length.ToString() + ")   " + user + "   " + fileInfo.FullName + " (" + fileInfo.FullName.Length.ToString() + ")"); });
        }
        // ----

        // You can also use the DirectoryInfo you created earlier here
        foreach (var dir in new di.GetDirectories("*", SearchOption.TopDirectoryOnly))
        {
            listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add(prefix + dir.Name + " (" + dir.Name.Length.ToString() + ")   "); });

            foreach (FileInfo fileInfo in dir.GetFiles())
            {
                listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add(prefix + fileInfo.Name + " (" + fileInfo.Name.Length.ToString() + ")   " + user + "   " + fileInfo.FullName + " (" + fileInfo.FullName.Length.ToString() + ")"); });
            }

            ScanFolder(prefix + "—", dir.FullName);
        }
    }
    catch
    {
        if (!this.IsDisposed)
        {
            listBox1.Invoke((MethodInvoker)delegate { listBox1.Items.Add("Access Denied to : " + path); });
        }
    }
}

Upvotes: 2

Related Questions