1

I'm confused on trying to display all .mdf files (and other database files) in a listview from a selected drive (ex. C:, D:) using a combobox (dropdown menu style).

Somehow the idea of the code escapes my mind

private void Form1_Load(object sender, EventArgs e)
{
    foreach (DriveInfo dir in DriveInfo.GetDrives())
        cmbDrive.Items.Add(dir.ToString());
}

private void btnScan(object sender, EventArgs e)
{
    ListViewItem item = new ListViewItem(Directory.GetFiles(cmbDrive.Text));
    string path = cmbDrive.Text;
    string extension = "*.mdf";
    lstvwdb2.Items.Add(Directory.GetFiles(path, extension));
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

Seems like you're messing things around a bit ...
Your list view item can take a string[] in the constructor, and your item collects of the list view should take a list view item in it's add method.

Try something like this:

// this is what you want to add to your lstvwdb2 items !!
string path = cmbDrive.Text;
string extension = "*.mdf";
ListViewItem item = new ListViewItem(Directory.GetFiles(cmbDrive.Text));
// You should add the above to your items collection
lstvwdb2.Items.Add(item);
Noctis
  • 11,507
  • 3
  • 43
  • 82
0

If you return an array of string (Directory.GetFiles) then you should loop over the returned list of filenames and add every single item one by one. If you try to add the whole array you will get an exception because the ListViewItemCollection.Add method cannot handle an array.

However, remember that Directory.GetFiles with only two parameters returns only the files that match your extension variable and are located EXACTLY in the folder expressed by the path variable (the root of the drive in your case). If any of your MDF files is located in a subfolder of that path variable it is not catched by the above GetFiles call. There is an overload of the GetFiles that takes an enum value instructing the GetFiles method to work recursively on all the underlying subfolders. Its use or not depends on your requirements

private void btnScan(object sender, EventArgs e)
{
    string path = cmbDrive.Text;
    string extension = "*.mdf";
    string[] files = Directory.GetFiles(path, extension);
    //string[] files = Directory.GetFiles(path, extension, SearchOption.AllDirectories);
    foreach(string s on files)
        lstvwdb2.Items.Add(s);
}

Beware that searching on the C: drive with the option AllDirectories could be problematic due to the presence of numerous reserved folders that cause an Exception if you try to read them.

To bypass this problem you could copy the code provided by Marc Gravell in its answer here and paste it in a utility class (for example a FileUts class), then loop using a foreach calling that method instead of Directory.GetFiles

private void btnScan(object sender, EventArgs e)
{
    string path = cmbDrive.Text;
    string extension = "*.mdf";
    foreach(string s in FileUts.GetFiles(path, extension))
        lstvwdb2.Items.Add(s);
}

public static class FileUts
{
    // Code provided by Marc Gravell
    public static IEnumerable<string> GetFiles(string root, string searchPattern)
    {
       .....
    }
}
Community
  • 1
  • 1
Steve
  • 213,761
  • 22
  • 232
  • 286
  • I was given this error : "Access to the path 'C:\$Recycle.Bin\S-1-5-18' is denied. " – NewbieInCoding Oct 18 '14 at 09:00
  • As I have said. The Recycle bin path is reserved and this is the problem with a AllDirectories. – Steve Oct 18 '14 at 09:03
  • Here there is a workaround http://stackoverflow.com/questions/4986293/access-to-the-path-is-denied-when-using-directory-getfiles – Steve Oct 18 '14 at 09:05
  • Is there any way i can avoid that? – NewbieInCoding Oct 18 '14 at 09:21
  • I did what you told me, the code works fine no errors however when i click the button(btnScan) it just freezes and does not display anything on the Listview – NewbieInCoding Oct 18 '14 at 12:38
  • Searching the C: drive could take a very long time. Be patient – Steve Oct 18 '14 at 12:39
  • I see. Also, i'm pretty sure i saved a database file in Drive D: , it should show a list of database files or .mdf but it's not doing it. – NewbieInCoding Oct 18 '14 at 12:40
  • Found 74 mdf files in my C: drive (many are from VS templates installation) in 140 secs on a 256GB SSD drive. Now testing on a production conventional hard-disk – Steve Oct 18 '14 at 12:45
  • I will test the code again. Maybe i'm just being impatient – NewbieInCoding Oct 18 '14 at 12:49
  • Found 47 mdf files in the conventional disk in 260 secs. To verify your result you could try to open a command prompt, CD to the root drive and then issue a `DIR *.MDF /S` – Steve Oct 18 '14 at 12:51