Sub DirList_OnPropertyChange()
form1.FileList.selectedIndex = -1
form1.submit()
End Sub
Sub FileList_OnPropertyChange()
form1.submit()
End Sub
</script>
</body>
</HTML>
The .aspx.vb File
There are 2 main subs here (other than the page_load event handler) that do all the work:
1. LoadLists - populates the file and folder lists by enumerating through the contents of the selected folder with the DirectoryInfo.GetFileSystemInfos() method.
2. LoadFileData - reads the contents of a selected file into an ArrayList using the StreamReader object, then loops through the ArrayList to populate the FileData ListBox.
Imports System.IO
' don’t forget to import System.IO or you won’t be able to use
' DirectoryInfo, FileSystemInfo as StreamReader classes
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents FileList As System.Web.UI.WebControls.ListBox
Protected WithEvents FileData As System.Web.UI.WebControls.ListBox
Protected WithEvents CurrentPath As System.Web.UI.WebControls.Label
Protected WithEvents DirList As System.Web.UI.WebControls.DropDownList
#Region " Web Form Designer Generated Code "
'<snip>
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim thisDir As String
Dim thisFile As String
If Page.IsPostBack Then
' we have access to the Request object
thisDir = Request("DirList")
thisFile = Request("FileList")
Else
thisDir = "C:\"
End If
If thisFile <> "" Then
' we know a file was selected because we get it’s name from
' the value posted to the page.
LoadFileData(thisFile, CurrentPath.Text)
Else
' no file was selected therefore a directory must
' have been selected, so load the new file and directory lists
' as per the contents of the selected directory.
LoadLists(thisDir)
End If
' make sure that nothing is already selected when the page loads.
DirList.SelectedIndex = -1
FileList.SelectedIndex = -1
End Sub
Sub LoadLists(ByVal thisDir As String)
' loop thru the directory and build the directory dropdownlist
' and the file list.
Try
Dim dir As DirectoryInfo = New DirectoryInfo(thisDir)
Dim fsi As FileSystemInfo
DirList.Items.Clear()
FileList.Items.Clear()
FileData.Items.Clear()
DirList.Items.Add("Select a subfolder...")
CurrentPath.Text = dir.FullName.ToString
For Each fsi In dir.GetFileSystemInfos()
Try
If Not TypeOf fsi Is FileInfo Then
' it must be a directory
Dim d As DirectoryInfo = CType(fsi, DirectoryInfo)
Dim dirName As String = Path.GetFullPath(d.FullName.ToString)
DirList.Items.Add(dirName)
Else
Dim d As FileInfo = CType(fsi, FileInfo)
Dim fileName As String = d.Name.ToString
FileList.Items.Add(fileName)
End If
Catch E As Exception
'skip this one and try the next...
End Try
Next fsi
Catch E As Exception
Response.Write(E.ToString())
Response.End()
End Try
End Sub
Sub LoadFileData(ByVal thisFile As String, ByVal thisDir As String)
' take the name of the current directory and the name of the
' selected file and open the file with a StreamReader object.
Try
FileData.Items.Clear()
Dim din As StreamReader = File.OpenText(thisDir & "\" & thisFile)
Dim str As String
Dim al As ArrayList = New ArrayList()
' Read the contents of the file into an arraylist then
' add each element of the arraylist to the FileData
' listbox.
Do
str = din.ReadLine()
If str <> Nothing Then
al.Add(str)
End If
Loop Until str = Nothing
Dim s As String
For Each s In al
FileData.Items.Add(s)
Next s
Catch E As Exception
Response.Write(E.ToString())
Response.End()
End Try
End Sub