Display Specific Directory on Web Form

Last post 08-25-2008 3:15 AM by surajsunshine. 4 replies.

Sort Posts:

  • Display Specific Directory on Web Form

    07-17-2008, 12:14 PM
    • Loading...
    • todaealas
    • Joined on 07-17-2008, 4:01 PM
    • Posts 2

    Hey everyone at ASP.NET,

    I am an amateur VB user, and i need to enquire on how to display a specific directory (along with sub-folders and files) neatly in a table. The ones I found online are either for CS, or it only displays the files, not sub-folders.

    For example, I want to show the sub-folders and files that are in this particular folder (~/Subject/). When I click on the folder, the files in the sub-folder will be displayed on another page. When I click on any file, it is available for download or viewing.

    I am using SQL Server Management Studio Express 2005, and Microsoft Visual Web Developer 2005 Express Edition. Coding is in VB.

    Please respond asap, as this is the only feature I have yet to add in my project.

  • Re: Display Specific Directory on Web Form

    07-21-2008, 4:55 AM

    Hi,

    You can use System.IO Namespace to get the folders or files under a specific directory, and then you can loop through them to display them. If it is folder, you can add Link to another page to list its sub-folders or files; if it is a file, you can use a link to point its path to allow user to download it.

    For example:

    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

    For more information, see http://aspnet101.com/aspnet101/tutorials.aspx?id=12

    Besides, you can use Converter (http://labs.developerfusion.co.uk/convert/csharp-to-vb.aspx) to convert C# code to VB.NET code easily.

     

    I look forward to receiving your test results.

     

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Display Specific Directory on Web Form

    07-21-2008, 11:47 AM
    • Loading...
    • todaealas
    • Joined on 07-17-2008, 4:01 PM
    • Posts 2

    Hi Thomas,

    Thanks for the reply.

    I tried the code. It worked nicely, but I want it to access my web directory - in this case, http://localhost:54005/xxx/Subject, not my local directory (C:\...).

    Also, I need it to just show a specified web folder (like http://localhost:54005/xxx/Subject), and not a string of drives.

    Thanks for taking the time to read this.

  • Re: Display Specific Directory on Web Form

    07-21-2008, 9:54 PM
    Answer

    Hi,

    Thanks for your response.

    You can use Server.MapPath Method that maps the specified relative or virtual path to the corresponding physical directory on the server.

    For more information, see http://msdn.microsoft.com/en-us/library/ms524632.aspx.

     

    I look forward to receiving your test results.

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Display Specific Directory on Web Form

    08-25-2008, 3:15 AM

    Hi..I try this but it not able to open the files. It can able to open only bmp files. why not other files? can u explain? its urgent

     

     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


Page 1 of 1 (5 items)
Microsoft Communities
Page view counter