views:

60

answers:

5

Hey Guys,

I was just wondering how do you display the conetnts of a chosen folder on a ListView or something for example so the files can be individually be selected (and multiple files)

At the moment i have a folder dialog where the user chooses their desired path and yeah have stopped there :S

Thanks,

Ash

A: 
System.IO.Directory.GetFiles(<filepath>)

will return a string array which you can iterate through and display file names. It can be passed a true boolean value as well if you wish to do a recursive directory search.

If you wish to display directories as well, you will need to use

System.IO.Directory.GetDirectories(<filepath>)
Jrud
+2  A: 

Given the string path you can use

Directory.GetDirectories

and

Directory.GetFiles

to retrieve the contents of a folder.

astander
Ahhh awesomee, seems like such a stupid question but ive been looking for it for too long and just got mental block loll.Thanks Guys!! :D
Ash
dangit :( what makes his answer so much better than mine? :( mine was even submitted 50 seconds earlier. Its his links isn't it? *sigh* defeated by his links...
Jrud
A: 

If you just call ListView.Items.AddRange(Directory.GetFiles(@"c:\temp"); the names of all files in c:\temp will be shown in the ListView.

scottm
A: 

All the cool kids use Linq :)

  var fileList = new DirectoryInfo(@"C:\").GetFiles().Where(file => file.Extension == ".txt");
        foreach (var file in fileList)
        { 
            // Do what you will here
           // listView1.Items.Add(
        }

This just gets text files in the C:\ drive, but you can tweak as necessary

RandomNoob
Thats is an amazing peice of code!! Thankyou so much i was trying to remember how to select only certain file types from a folder but could remember, is there a waqy of selecting multiple types but specifying them?Thanks,Ash
Ash
+2  A: 

I'm going to focus on your statement : "a Listview or something," and talk about the "something" scenario :)

Why aren't you using the built-in control 'OpenFileDialog : you can set the 'MultiSelect property to 'true and select all the files you like, you can filter the files that appear in complex ways, etc. : it's there, it's "free," it works.

If you specifically do not want to use this control for reasons like, for example, you want the list files to remain visible (i.e., not to be a modal interface) at all times, I suggest you clarify your original question to reflect that. The more you tell us exactly what you want, the more focused the answers you can get.

regards Bill,

BillW
Well the reason i didnt use the OpenFileDialog is because the files arent actually being opened they are being uploaded to a server, all i need to collect from the folder is the location and then the name and possibly some small over details then just upload the file to a server, the files can be multiply selected from loads of different folders which can all be viewed in the one list box.I think im using the right method still right?Ash
Ash