File System List With System.IO

This tutorial is more a code sample than a tutorial, but we can’t create an online example of it due to security issues.

Basically, what you see here is a use of the System.IO namespace to get the file structure of your PC. To get the list of Logical Drives, we use the GetLogicalDrives method. From there, click on the drive you want to get the file structure of that drive. It will list the folders and files from that drive. Naturally, like Explorer, you can drill down as far as you wish here. As you will also see, we show how to display certain files, depending on the file’s extension. In this case, we use GIF or JPG files. Each of them should show up as a link. Click on that link and it will open a new window with that image. To download the images used in this tutorial, use these images (folder File System List With  System.IO – file File System List With  System.IO – up File System List With  System.IO) and put them in an images folder.

Like we said earlier, there is no online example, but all you’ll need to do is to copy this code to a new page on your system, and run it from a file server. Here’s the code:

<%@ Import Namespace="System.IO" %>
<html>
<head>
	<meta name="GENERATOR" Content="ASP Express 2.1">
	<title>Drives/Folders/Files</title>
<script language="VB" runat="server">
Dim sSystemDrives as String()
Dim intNumberOfDrives as Integer
Dim sDrive
Dim strParent As String
Dim sDriveList as string
Sub Page_Load(Source As Object, E As System.EventArgs)

Dim drives As String() = Directory.GetLogicalDrives()
Dim count As Integer = drives.Length
Dim i As Integer
For i = 0 To count - 1
	sDriveList += "<spandobig""><a href=Files.aspx?d=" & drives(i) & " class='dobig'>" & _
	drives(i) & " </a>   </span> "
Next i
Literal1.text+=sDriveList
Literal1.text += "<hr>"

if request.Querystring("d") <> "" then
	Dim d as String
	d=request.querystring("d")
	GetFiles()
End If
End Sub

Sub GetFiles()

   Try
      Dim strDir As String = Request.QueryString("d")
	strParent = strDir.Substring(0, strDir.LastIndexOf("\"))
	'strParent += strParent.EndsWith(":") ? "\\" : ""
if strParent.endswith(":") then
strParent=strParent & "\"
End If
      upLink.NavigateUrl = "files.aspx?d=" + strParent
      txtCurrentDir.Text = "Address: <b>" + strDir + "</b>"
      Dim DirInfo As New DirectoryInfo(strDir)
      Dim subDirs As DirectoryInfo() = DirInfo.GetDirectories()
      Dim Files As FileInfo() = DirInfo.GetFiles()
      txtFileList.Text = "<table border=""0"" width=""50%"">"
      Dim i As Integer
      For i = 0 To subDirs.Length - 1
	txtFileList.Text += "<tr><td><img src='images/folder.gif'><a href=""files.aspx?d=" & _
	subDirs(i).Fullname & _
	chr(34) &">" & subDirs(i).Name & "</a></td><td valign='bottom'>" & _
	subDirs(i).LastWriteTime & "</td></tr>"
      Next i
      For i = 0 To Files.Length - 1
	if right(Files(i).Name, 3) = "gif" or right(Files(i).Name, 3) = "jpg" then
		txtFileList.Text += "<tr><td><img src='images/file.gif'><a href='" & _
		strDir & "/" &  Files(i).Name & "' target='_blank'>" & _
		Files(i).Name & "</a></td><td valign='bottom'>" & _
		Files(i).LastWriteTime & "</td></tr>"

	else
		txtFileList.Text += "<tr><td><img src='images/file.gif'>" & _
		Files(i).Name & "</td><td valign='bottom'>" & _
		 Files(i).LastWriteTime & "</td></tr>"
	End If

      Next i
      txtFileList.Text += "</table>"

   Catch
      txtFileList.Text = "Error retrieving directory info - Check Drive ("  & strParent & ")"
   End Try

End Sub

</script><STYLE TYPE="text/css">
<!--
BODY {
	font-family : Arial;
	font-size : 10pt;
}
TD {
	font-family : Arial;
	font-size : 10pt;
}
.DoBig {
	font-family : Verdana;
	font-size : 12pt;
	font-weight : bold;
}
-->
</STYLE>
</head>
<body>
<div align="center"><asp:Literal ID="literal1" text="<b>Drives:</b><br>" runat="server" /></div>
<form runat="server">
<asp:HyperLink id="upLink" runat="server" ImageUrl="images/up.gif"/>
<br/>
<asp:Label id="txtCurrentDir" Font-Name="MS Sans Serif" Font-Size="8pt" runat="server"/>
<br><br>
<asp:Label id="txtFileList" Font-Name="MS Sans Serif" Font-Size="8pt" runat="server"/>
</form>
</body>
</html>

Related Posts:

  • No Related Posts
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!