 |
| Visual Basic 2008 Professionals For advanced Visual Basic coders working in version 2008. Beginning-level questions will be redirected to other forums, |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Visual Basic 2008 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

August 26th, 2010, 07:15 AM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Files and Folders in VB.Net2008
Hi,
I'm new to VB.Net, i have a situation like,you have folder name "Arrange", inside this, it contains so many "txt" and "zip" files, you can rearrange it by right clicking and select "Arrange By , Last modified", i have to access this option through code and have to do this every time while i'm looping or entry of the function.
I can do this in program through forEach loop and get details like filename and creationDate, but i want to arrange it in "Arrange" Folder.
Please help me out.
Thanks in Advance:-)
Last edited by kumar.selva.c; August 27th, 2010 at 05:41 AM..
|
|

August 27th, 2010, 07:02 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
You can use a combination of DirectoryInfo and LINQ to get files from the file system and group them by something, such as a file extension. Here's a quick example that lists all files in a folder, each file grouped under an Extension header:
Code:
Dim folder As String = "C:\SomePath"
Dim directoryInfo As New IO.DirectoryInfo(folder)
Dim allFiles = From f In directoryInfo.GetFiles() _
Group f By Extension = f.Extension Into Group _
Select Extension = Extension, Files = Group
For Each fileGroup In allFiles
' Write out the extension for the group
Console.WriteLine(fileGroup.Extension)
For Each file In fileGroup.Files
' Write out each individual file name in the group
Console.WriteLine(" " & file.Name)
Next
Next
For more information about this, check out the System.IO namespace and the 101 LINQ examples: http://msdn.microsoft.com/en-us/vbasic/bb688088.aspx
You can do similar stuff for other groups, like date created, first letter of the filename and so on.
Cheers,
Imar
|
|

August 27th, 2010, 07:04 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
BTW, the output of this code will be something like this:
Code:
.docx
SomeFile1.docx
SomeFile2.docx
.txt
SomeFile1.txt
SomeFile2.txt
SomeFile3.txt
.zip
SomeFile1.zip
SomeFile2.zip
|
|

August 28th, 2010, 02:49 AM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Files and Folders
Hi Imar,
Thanks for your kind reply, and mine is little bit different situation,
see, jus create a folder named "Arrange" or wat eve u like, and put some "txt" and "zip" files with different creation time of each file.
Do right click and select arrange by lastmodified, so that all files will be arranged by creation date and Time. This is the folder option.
I want to access this option through coding, so that i can aovid so many lines of coding.
could u plz help me out this?
Thanks in advance.
Regards,
Selva
|
|

August 28th, 2010, 04:01 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi there,
I am not sure what you're asking. This is a Windows UI feature, so how would you access this through code? If this is about a UI and selecting files, you can use an OpenFileDialog which supports this by default.
Otherwise, I think you need to code it yourself.
Cheers,
Imar
|
|
The Following User Says Thank You to Imar For This Useful Post:
|
|
|

September 1st, 2010, 06:14 AM
|
|
Registered User
|
|
Join Date: Aug 2010
Posts: 6
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Files and Folders
Thanks Imar 
|
|

September 26th, 2011, 12:58 PM
|
|
Registered User
|
|
Join Date: Sep 2011
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
List Files in a Directory
List Files in a Directory
A little code snippet that lists files in a directory.
Quote:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' make a reference to a directory
Dim di As New IO.DirectoryInfo("c:\")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
ListBox1.Items.Add(dra)
Next
End Sub
|
To filter search change di.GetFiles() to di.GetFiles(â.extionsionâ)
|
|
 |