Lanc1988 Posted December 8, 2003 Posted December 8, 2003 I have a list box and I want it to add all items in a specified folder. I have this so far, but I can't figure out how to get the filenames part right: ListBox1.Items.Add(Application.StartupPath & "\Program Images\Image Viewer\" & filename.Text) Also, when its displaying that in the listbox, its showing the whole directory (ex. C:\Files\Folder\) I would like it to just show the filename with the ending (ex. Picture.bmp or Text.txt) Thanks in advance Quote
Leaders Iceplug Posted December 8, 2003 Leaders Posted December 8, 2003 Have you looked at the System.IO.Directory.GetFiles() function? It will fill an array with all of the files in the folder. :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Lanc1988 Posted December 8, 2003 Author Posted December 8, 2003 hmm.. I tried this: ListBox1.Items.Add(System.IO.Directory.GetFiles(Application.StartupPath & "\Program Images\Image Viewer\")) And now it displays System String[] in the list box and when clicked still doesn't show the file.. Am i using the System.IO.Directory.GetFiles() function right? if not whats wrong? Quote
Leaders Iceplug Posted December 9, 2003 Leaders Posted December 9, 2003 Use the .AddRange method of the ListBox. :) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
Mehyar Posted December 9, 2003 Posted December 9, 2003 And now it displays System String[] in the list box and when clicked still doesn't show the file.. Am i using the System.IO.Directory.GetFiles() function right? if not whats wrong? [/Quote] This is because the method GetFiles() returns an array of strings not a single item and the Add method you are using accepts one item only. That's why as IcePlug said use the AddRange method instead of the Add. You can always loop through the array and Add the items one by one if you prefer, but I would go with the AddRange.... Quote Dream as if you'll live forever, live as if you'll die today
Lanc1988 Posted December 9, 2003 Author Posted December 9, 2003 Ok its works perfectly, except it still shows the whole file path to the file. Is there something I can add to it to just display the file name in the list (Example: File.htm or Picture.bmp) Quote
HJB417 Posted December 10, 2003 Posted December 10, 2003 Ok its works perfectly, except it still shows the whole file path to the file. Is there something I can add to it to just display the file name in the list (Example: File.htm or Picture.bmp) ya, use the FileInfo class and the Name property. Quote
*Gurus* divil Posted December 10, 2003 *Gurus* Posted December 10, 2003 Or System.IO.Path.GetFileName() Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Lanc1988 Posted December 11, 2003 Author Posted December 11, 2003 It under lines it all in blue if i put this: ListBox1.Items.AddRange(System.IO.Path.GetFileName(Application.StartupPath & "\Program Images\Image Viewer\")) Quote
HJB417 Posted December 11, 2003 Posted December 11, 2003 AddRange takes an Array which GetFileName does not return. try something like this (C#) foreach(string filename in System.IO.Directory.GetFiles(Application.StartupPath & "\Program Images\Image Viewer\")) ListBox1.Items.Add(System.IO.Path.GetFileName(filename)); Quote
Lanc1988 Posted December 11, 2003 Author Posted December 11, 2003 I only have vb.net 2003 I want it to get a list of all files in the specified folder tho, i dont want to enter each file thats suppose to be in it cuz i want the user to be able to add files to the folder and view them through the program. Quote
*Gurus* divil Posted December 11, 2003 *Gurus* Posted December 11, 2003 Yes, you use GetFiles to get your array of files, the GetFileName on all those entries to convert them to just the filename instead of the whole path. Then you feed that array to the listbox's AddRange function. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Lanc1988 Posted December 11, 2003 Author Posted December 11, 2003 Thats what im having trouble with, I dont know how.. Quote
Leaders dynamic_sysop Posted December 12, 2003 Leaders Posted December 12, 2003 try this... Dim Allfiles As String() = IO.Directory.GetFiles("C:\") Dim strfile As String For Each strfile In Allfiles Dim filename As New IO.FileInfo(strfile) '/// this will give you the file name etc... ListBox1.Items.Add(filename.Name) Next Quote
Lanc1988 Posted December 13, 2003 Author Posted December 13, 2003 Works very nice, its perfect. Now if this is possible, is there a way to only allow certain files to be in the list? Like all image files and .html files? If its to complicated, just post that it is otherwise if its farly simple please post it, thx. Quote
Administrators PlausiblyDamp Posted December 13, 2003 Administrators Posted December 13, 2003 The GetFiles method can take a 2nd parameter to specifiy the file extension. dim htmlfiles as string = IO.Directory.GetFiles("C:\","*.html") Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.