Reputation: 53
So I'm trying to make this Management System for other programs using VB.Net
I asked another question but it was too vague so I'm still stuck.
I got a List View (Which I was recommended to use instead of Listbox) that finds a set of files and extensions in a folder I've chosen in my code (Static, so it finds the same folder each time)
I would like to be able to press "Set.." (A button I already have created and placed) able to open FileDialog, to choose a folder. When pressing OK, the "Application" remembers what folder/directory you were in, so you don't need to set the folder each time. Also that you can even close down the application, shut down your PC, but the code will still remember what directory/folder you were in, for later use.
Code;
' This code right here makes the Update button of mine, able to find the files inside the folder/directory. Nothing more. Just refreshes list, as List View is blank when the form is opened.
Private Sub updateButtonGame_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updateButtonGame.Click
If FolderBrowserDialog1.SelectedPath = "xxxx\xxxx\xxxx" Then
' List files in the folder.
ListFiles(FolderBrowserDialog1.SelectedPath)
End If
End Sub
' This code makes the listview able to find files with multiple Extensions which I need to find.
Private Sub ListFiles(ByVal folderPath As String)
ListViewGame.Items.Clear()
Dim fi = From f In New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath).GetFiles().Cast(Of IO.FileInfo)() _
Where f.Extension = ".z64" OrElse f.Extension = ".nds" OrElse f.Extension = ".BIN" OrElse f.Extension = ".smc" OrElse f.Extension = ".ISO" OrElse f.Extension = ".nes" OrElse f.Extension = ".gb"
Order By f.Extension
Select f
For Each fileInfo As System.IO.FileInfo In fi
ListViewGame.Items.Add(fileInfo.Name)
Next
End Sub
So after the Set button has done it's job, and "remember" what directory you pressed OK in, it adds the code to the Update-button code so by pressing Update, it does the same as before.
Any good suggestions?
Upvotes: 1
Views: 267
Reputation: 216303
You should add to your settings (Project->Properties->Settings) a new entry called LastUsedPath
(or whatever you like). This setting should be of type string and user scope. You could also set an initial value that points to your application folder. (".\")
Now in code, everytime you click the button updateButtonGame
you read this value and set the FolderBrowserDialog
SelectedPath property to this value.
In this way the dialog opens initially to this folder, then, if the user confirms the selection or change to another directory, you could change the settings to the new path and store it for the next time.
using fbd = new FolderBrowserDialog()
fbd.SelectedPath = My.Settings.LastUsedPath
If fbd.ShowDialog() = DialogResult.OK Then
ListFiles(fbd.SelectedPath)
My.Settings.LastUsedPath = fbd.SelectedPath
My.Settings.Save()
End If
End Using
I suggest also to change the enumeration of file to use the passed variable and not a reference to the FolderBrowserDialog.
For Each fileInfo in From f In New IO.DirectoryInfo(folderPath).EnumerateFiles _
Where f.Extension = ....
Notice also that instead of using GetFiles I have changed to EnumerateFiles. This is better because it allows to start the enumeration without loading the whole collection of FileInfo in memory
See EnumerateFiles remarks section
EDIT My final example was a replacement for your FOR NEXT that populates the ListView. You use mine approach or yours.
Private Sub ListFiles(ByVal folderPath As String)
For Each fi In From f In New IO.DirectoryInfo(folderPath).EnumerateFiles _
Where f.Extension = ".z64" OrElse f.Extension = ".nds" _
OrElse f.Extension = ".BIN" OrElse f.Extension = ".smc" _
OrElse f.Extension = ".ISO" OrElse f.Extension = ".nes" _
OrElse f.Extension = ".gb"
Order By f.Extension
Select f
' Remove this line
' For Each fileInfo As System.IO.FileInfo In fi
ListView1.Items.Add(fi.Name)
Next
End Sub
Upvotes: 1