|
-
Jul 13th, 2009, 03:51 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] List Directory and Arrange by type
Hi,
Is it possible to achieve that by using Directory class?
I tried this method by http://www.java2s.com/Code/VB/File-D...riesinroot.htm and it seems to list my folders by date.
Please advise.
Thanks
-
Jul 13th, 2009, 03:55 AM
#2
Re: List Directory and Arrange by type
If you mean get the all the file names from a folder and then sort by the extension then yes, it's possible. You might want to follow the blog link in my signature about sorting arrays and collections. You can call Directory.GetFiles or DirectoryInfo.GetFiles to get an array of Strings or FileInfos respectively and then use a Comparison or IComparer to sort by file extension. For strings you can use Path.GetExtension to get each file extension and for FileInfos you can use their Extension property.
-
Jul 13th, 2009, 05:38 AM
#3
Re: List Directory and Arrange by type
This is the way I would do it, which is pretty much what JMC said but just thought I would provide a working example to help. This is just a console application but obviously you could modify it to work with any .NET app very easily.
vb Code:
'This is the function that compares the extensions using
'String.Compare on the file extensions
Private Function ExtensionCompare(ByVal x As String, ByVal y As String) As Integer
Return String.Compare(IO.Path.GetExtension(x), IO.Path.GetExtension(y))
End Function
Sub Main()
'Get the list of files in the C drive root
Dim FileList As List(Of String) = IO.Directory.GetFiles("C:\").ToList
'Sort the files using the ExtensionCompare function
'which is defined above
FileList.Sort(New Comparison(Of String)(AddressOf ExtensionCompare))
'Loop through the list from start to end and write each
'value to the screen
For i As Integer = 0 To FileList.Count - 1
Console.WriteLine(FileList(i))
Next
'Pause
Console.ReadLine()
End Sub
Last edited by chris128; Jul 13th, 2009 at 01:20 PM.
-
Jul 13th, 2009, 10:50 AM
#4
Thread Starter
Hyperactive Member
Re: List Directory and Arrange by type
Thanks both for replying I tried the 2nd coding and this line seems to have error:
Dim FileList As List(Of String) = IO.Directory.GetFiles("C:\").ToList
As It doesnt seems to have ToList method.
-
Jul 13th, 2009, 10:56 AM
#5
Re: List Directory and Arrange by type
It does have that method as I compiled and ran that code before I posted it. You must have changed something from the code that I posted - either that or perhaps you are using a different version of the framework (but you marked this thread as 2008 so I assume not).
Can you post your code?
-
Jul 13th, 2009, 01:12 PM
#6
Thread Starter
Hyperactive Member
Re: List Directory and Arrange by type
I tried console application and it works without error, for my win32 app, it shows 'ToList' is not a member of system.array
-
Jul 13th, 2009, 01:16 PM
#7
Re: List Directory and Arrange by type
What do you mean by Win32 app? It works fine in a windows forms app (VB.NET) when I just tested it. You're not using C++ are you?
Last edited by chris128; Jul 13th, 2009 at 01:21 PM.
-
Jul 13th, 2009, 04:02 PM
#8
Re: List Directory and Arrange by type
Are you compiling to .NET Framework 3.5, or to 2.0? Not 100% sure if that makes a difference, but it might. Isn't ToList an extension method?
-
Jul 13th, 2009, 04:17 PM
#9
Re: List Directory and Arrange by type
Ah yeah that method doesnt exist for .NET 2.0 apps but as the OP marked this as a VS 2008 thread and didnt indiciate he wasnt using 3 / 3.5 I foolishly assumed he was using that lol
Thats one thing I have often wondered - wouldnt it be better for threads to be marked with their .NET framework version rather than the version of VS used? As the framework version has a much bigger bearing on what can be done than VS does (I guess this wasnt a problem until 2008 came out as that introduced the ability to target specific versions of the framework)
-
Jul 13th, 2009, 05:29 PM
#10
Re: List Directory and Arrange by type
 Originally Posted by chris128
... wouldnt it be better for threads to be marked with their .NET framework version rather than the version of VS used?
Agreed!
-
Jul 14th, 2009, 02:53 AM
#11
Re: List Directory and Arrange by type
-
Jul 14th, 2009, 08:41 AM
#12
Thread Starter
Hyperactive Member
Re: List Directory and Arrange by type
I am using .NET Framework 3.5 SP1. The weird part in my console app no problem but not my win32. Maybe I try recreate my project again.
-
Jul 14th, 2009, 09:29 AM
#13
Re: List Directory and Arrange by type
What do you mean by win32? Is this a VB.NET project or a C++ project?
-
Jul 14th, 2009, 09:38 AM
#14
Re: List Directory and Arrange by type
Are you sure your WinForms app (not Win32, which just means 32-bit Windows) is not targeting .NET 2.0 or 3.0? Also, check that you haven't removed the required reference or import from your project. The assembly is System.Core.dll and the namespace is System.Linq.
-
Jul 14th, 2009, 09:43 AM
#15
Thread Starter
Hyperactive Member
Re: List Directory and Arrange by type
I tried create a new blank win32 and it works. Since you all were talking about the .NET Framework, I suspect that the project that I'm working on is running the old .NET Framework?
As I think I have already do the conversion, but can I compile it as 3.5 or add some reference to make sure the .ToList is recognize?
-
Jul 14th, 2009, 09:52 AM
#16
Thread Starter
Hyperactive Member
Re: List Directory and Arrange by type
Its a Windows Form VB.NET project. I can't import System.LINQ, I think the problem is really related to .NET Framework. As I try to add a reference of System.Core, its greyed out and also other 3.5 references.
Is there a way to convert to 3.5 from 2.0? I remember I did went through the conversion process.
-
Jul 14th, 2009, 09:58 AM
#17
Re: List Directory and Arrange by type
It's obviously not targeting .NET 3.5 at the moment so you must have chosen 2.0 when you created the project. You can change the target version from the Advanced Compile Options in the project properties.
-
Jul 14th, 2009, 10:05 AM
#18
Re: List Directory and Arrange by type
Just open up your project properties (double click My Project in Solution Explorer) and go to the Compile section, click Advanced and then change the target type from .NET 2 to 3.5 
EDIT: Looks like JMC beat me to it
-
Jul 14th, 2009, 10:25 AM
#19
Thread Starter
Hyperactive Member
Re: List Directory and Arrange by type
Hi all, thanks it works. You 2 have been a great help. Btw, how should I sort it by alphabetically order because the directory I am testing inside have the same extension.
-
Jul 14th, 2009, 10:28 AM
#20
Re: List Directory and Arrange by type
I believe that my blog post covers sorting by two properties. You can use the Path class to get the extension and the file name, or the FileInfo class does the same.
-
Jul 14th, 2009, 11:04 AM
#21
Re: List Directory and Arrange by type
The code I posted in Post #3 does exactly that doesnt it?
-
Jul 14th, 2009, 11:09 AM
#22
Thread Starter
Hyperactive Member
Re: List Directory and Arrange by type
I list my C:\ and that's the output
bootmgr
autoexec.bat
pagefile.sys
hiberfile.sys
config.sys
I think is arrange by type, I want to arrange by ascending order as well.
-
Jul 14th, 2009, 12:41 PM
#23
Re: List Directory and Arrange by type
Ahhh I see, sorry I thought you just meant the type needed to be in alphabetical order (as you did not specify).
I'm sure you could modify the code I posted to incorporate such functionality but I am not quite sure how and I'm afraid I havent got time at the moment to have a play around
-
Jul 14th, 2009, 01:19 PM
#24
Thread Starter
Hyperactive Member
Re: List Directory and Arrange by type
Yup, coz at first I thought that will be some built in method to arrange by type/date/ etc. Didn't know that need to code extra method, I think I would take a look at the sorting part. But nevertheless, thanks alot for the help
-
Jul 16th, 2009, 04:50 AM
#25
Thread Starter
Hyperactive Member
Re: List Directory and Arrange by type
while reading jmcilhinney blog, I found out that actually the .Sort method helps me sort in ascending order for alphanumeric. That's good. Thanks alot
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|