troubleshooting Question

Listing file names within all folders?

Avatar of andyw27
andyw27 asked on
4 Comments1 Solution279 ViewsLast Modified:

I using code from the following website:

I've simplified abit to aid my understanding.  The code below works however I tried changing this line:

filCustom = dirCustom.GetFiles();

to this

filCustom = dirCustom.GetFiles(SearchOption.AllDirectories);

But it gives this error:

Error      1      The best overloaded method match for 'System.IO.DirectoryInfo.GetFiles(string)' has some invalid argument

I would like to list all of the files within any given folder, regardless of however many sub folders there were.  It would also be bonus if I could append the file name being printed with the full path.

Appreciate any ideas on how I might accomplish this.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FileListing
    public partial class Form1 : Form
        public Form1()
            // Set the default path
            string path = Directory.GetCurrentDirectory();
            lbldirectory.Text = path;
        private void btnList_Click(object sender, EventArgs e)
            DirectoryInfo dirCustom = new DirectoryInfo(lbldirectory.Text);
            FileInfo[] filCustom;
            filCustom = dirCustom.GetFiles();
            foreach (FileInfo filFile in filCustom)
                txtFiles.AppendText(filFile.Name + "\r\n");
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros