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 :)
Printable View
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 :)
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.
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
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.
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?
I tried console application and it works without error, for my win32 app, it shows 'ToList' is not a member of system.array
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?
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?
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)
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.
What do you mean by win32? Is this a VB.NET project or a C++ project?
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.
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?
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.
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.
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
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.
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.
The code I posted in Post #3 does exactly that doesnt it?
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.
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
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 :)
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 :)