VB .net to read all files exist in folder
In my previous post you already see how do i write code to browse and select folder. Today i will show i explored the way to read all file name in a folder, which i select only a top folder. It means that it will read all files in my selected folder but application will not read any files which in its subfolders.
Generally code to browse files and folder are the same so i will not show you how to browse files but just show you what code i use to read file name which that folder.
Through searching through VB help viewer as well as some forum i look, i understand that in VB .net i can have it to read all file information through following code:
Through that code system will read all file name "For each filename as string" within folder "In My.Computer.FileSystem.Getfiles(newDei,Microsoft.VisualBasic.FileIO.SearchOption.
SearchTopLevelOnly". There are a few notes here:
We can declare object within the code as you can see "For Each filename As string In..."
For need to have next otherwise it will result in error.
"Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly" is a search option that we provide, in case you want application to search subfolder you can change it to "SearchAllSubDirectories".
With above code application will read all of your files in that selected folder, but you need to know what do the application found? so i apply following code:
Actually you can use "filename" as we already declare and provide it the value, however in this case application will show you path and filename, but in above code my intention is to show only file name, that is why i add "My.computer.filesystem.getname" more.
In some case you may want to select some specific file from file browser, in this case you can change code to following:
Where "Filter" has a role to limit what type of file do you want to see, in case you want to see all type of file you can change above code from "|*.mdb" to "|*.*". And if you want to have a possibility to select many files you can change "Multiselect=False" to "Multiselect=True"
Generally code to browse files and folder are the same so i will not show you how to browse files but just show you what code i use to read file name which that folder.
Using OpenDailog As New FolderBrowserDialog With OpenDailog If OpenDailog.ShowDialog() = Windows.Forms.DialogResult.OK Then Dim newDir As String = .SelectedPath My.Computer.FileSystem.CreateDirectory(newDir) For Each filename As String In My.Computer.FileSystem.GetFiles(newDir, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly) MsgBox(My.Computer.FileSystem.GetName(filename)) Next End If End With End Using
Through searching through VB help viewer as well as some forum i look, i understand that in VB .net i can have it to read all file information through following code:
For Each filename As String In My.Computer.FileSystem.GetFiles(newDir, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly) Next
Through that code system will read all file name "For each filename as string" within folder "In My.Computer.FileSystem.Getfiles(newDei,Microsoft.VisualBasic.FileIO.SearchOption.
SearchTopLevelOnly". There are a few notes here:
With above code application will read all of your files in that selected folder, but you need to know what do the application found? so i apply following code:
MsgBox(My.Computer.FileSystem.GetName(filename))
Actually you can use "filename" as we already declare and provide it the value, however in this case application will show you path and filename, but in above code my intention is to show only file name, that is why i add "My.computer.filesystem.getname" more.
In some case you may want to select some specific file from file browser, in this case you can change code to following:
Using OpenDialog As New OpenFileDialog With OpenDialog .Filter = "MS Access Files (*.mdb)|*.mdb" .Title = "Select the Destenation DB to Execute the Queries" .Multiselect = False If OpenDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then msgobox (.FileName) End If End With End Using
Where "Filter" has a role to limit what type of file do you want to see, in case you want to see all type of file you can change above code from "|*.mdb" to "|*.*". And if you want to have a possibility to select many files you can change "Multiselect=False" to "Multiselect=True"
I was barely amazed at how you had written this content. Please keep posting.
ReplyDelete.asp.net training in chennai
.net coaching centre in chennai