locked
please have a glance ,i want to display all the drives and folders using vb.net RRS feed

  • Question

  • User123544528 posted

    please have a glance, i want to display all the drives and folders ,  and user can select the file also

    i got this code from one of forum, but it is showing all the files but not folders and directives,

     can any one help?

    Imports

     

    System.IO

    Imports System.Data

    Partial

     

     

    Class _DefaultInherits System.Web.UI.PagePrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    ' make a reference to a directory

     

     

     

     

    Dim di As New IO.DirectoryInfo("c:\")

    Dim diar1 As IO.FileInfo() = di.GetFiles()

    Dim dra As IO.FileInfo

    'list the names of all files in the specified directory

    ListBox1.Items.Add(dra.fullname)

     

     

     

    For Each dra In diar1Next

    End

     

    End Sub Class

     

    can anybuddy help, how to display all the drives and folders using vb.net 2005

    Saturday, May 8, 2010 8:17 AM

Answers

  • User1006193418 posted

    Hi,

    You can use My namespace.

            For Each drName As DriveInfo In My.Computer.FileSystem.Drives()
                For Each folderName As String In My.Computer.FileSystem.GetDirectories(drName.ToString)
                    Dim folderInfo As DirectoryInfo = New DirectoryInfo(folderName)
    
                Next
            Next

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 10, 2010 4:20 AM
  • User1006193418 posted

    Hi,

    This is because you are trying to visit drivers which are not ready, like DVD drivers.

    To avoid this error, we need to use DriveInfo.IsReady property. Here is the complete code of this task.

            For Each drInfo As IO.DriveInfo In My.Computer.FileSystem.Drives()
                Response.Write(drInfo.Name() & "<br />")
                If Not drInfo.IsReady Then
                    Continue For
                End If
                For Each folderName As String In My.Computer.FileSystem.GetDirectories(drInfo.ToString)
                    Dim folderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(folderName)
                    Response.Write("&nbsp;&nbsp;&nbsp;&nbsp;" & folderInfo.FullName & "<br />")
                Next
                Response.Write("<br />")
            Next

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 10, 2010 5:01 AM

All replies

  • User-1659704165 posted

    Hi,

    Dim filenames() As String = Directory.GetFiles("C:\Re_Class")
    Dim i, o As Integer
    Dim filename As String

    For i = 0 To filenames.Length - 1
    filename = Path.GetFileName(filenames(i))
    lstOne.Items.Add(filename)
    Next i

    chk above  Snippet

    Saturday, May 8, 2010 10:16 AM
  • User123544528 posted

    hi, thankx , but i want to show all the drives and  folders now

    please help 

    <FORM id=form1 name=form1 action=Default.aspx method=post>
    how to display drives and all folders, please help with some code?
    thank u
     
     
      
     

    </FORM>

     

     

    Monday, May 10, 2010 1:08 AM
  • User1006193418 posted

    Hi,

    You can use My namespace.

            For Each drName As DriveInfo In My.Computer.FileSystem.Drives()
                For Each folderName As String In My.Computer.FileSystem.GetDirectories(drName.ToString)
                    Dim folderInfo As DirectoryInfo = New DirectoryInfo(folderName)
    
                Next
            Next

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 10, 2010 4:20 AM
  • User123544528 posted

    im getting error, im using vb.net, website

    Error: Type 'DriveInfo' is not defined, im getting suggestion to change it to IO.DriveInfo

    then the code will be like this

     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click' make a reference to a directory

      Dim di As New IO.DirectoryInfo("C:\")Dim diar1 As IO.DirectoryInfo() = di.GetDirectories()Dim da As IO.DirectoryInfo'list the names of all files in the specified directory

     For Each drName As IO.DriveInfo In My.Computer.FileSystem.Drives()For Each folderName As String In My.Computer.FileSystem.GetDirectories(drName.ToString)Dim folderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(folderName)Next

    Next

    End Sub

    then also im getting error:The device is not ready.IOException was unhandled by user code

     

    and if im using only ur for statement , then also im getting same error

     

     

    Monday, May 10, 2010 4:29 AM
  • User1006193418 posted

    Hi,

    This is because you are trying to visit drivers which are not ready, like DVD drivers.

    To avoid this error, we need to use DriveInfo.IsReady property. Here is the complete code of this task.

            For Each drInfo As IO.DriveInfo In My.Computer.FileSystem.Drives()
                Response.Write(drInfo.Name() & "<br />")
                If Not drInfo.IsReady Then
                    Continue For
                End If
                For Each folderName As String In My.Computer.FileSystem.GetDirectories(drInfo.ToString)
                    Dim folderInfo As IO.DirectoryInfo = New IO.DirectoryInfo(folderName)
                    Response.Write("&nbsp;&nbsp;&nbsp;&nbsp;" & folderInfo.FullName & "<br />")
                Next
                Response.Write("<br />")
            Next

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 10, 2010 5:01 AM
  • User123544528 posted

    thank u dude, yes it is workine fine,

    how to generate this folders and drives in tree view

    how to get the sub folders? and

    is their any way such tat the displayed folders can b selected?

    Monday, May 10, 2010 5:48 AM
  • User1006193418 posted

    Hi,

    It sounds like you are trying to achieve a web based file management. If so, I suggest you refer to these links for more help and demos.

    http://www.codeproject.com/KB/aspnet/WebFileManager.aspx

    http://www.codeproject.com/KB/applications/ASPNetFileManagement.aspx

    http://www.essentialobjects.com/Products/EOWeb/FileExplorer.aspx

    http://www.webresourcesdepot.com/asp-net-file-manager-windows-explorer-like-izwebfilemanager/ 

    Best Regards,
    Shengqing Yang

    Monday, May 10, 2010 10:52 PM
  • User123544528 posted

    vb.net 2005

    THIS IS mycode(working fine) to display all the drives and folders in tree view, now i want to add a small thing to it, i.e

    i want to display this tree view in listbox, can any one help?

    thanx

    PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If Not IsPostBack Then
                TreeView1.Nodes.Clear()
                For Each d As System.IO.DriveInfo In My.Computer.FileSystem.Drives()
                    Dim dn As New TreeNode(d.Name)
                    TreeView1.Nodes.Add(dn)
                    If d.IsReady Then
                        For Each din As String In My.Computer.FileSystem.GetDirectories(d.Name)
                            Dim di As New System.IO.DirectoryInfo(din)
                            Dim tn As New TreeNode(di.Name)
                            dn.ChildNodes.Add(tn)
                            tn.ChildNodes.Add(New TreeNode("")) 'Empty node to create + sign  
                        Next
                    End If
                Next
                TreeView1.CollapseAll()
            End If

        End Sub

    Protected Sub button2_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    TextBox1.Text = TreeView1.SelectedNode.ValuePath

     End Sub

    Tuesday, May 11, 2010 4:24 AM
  • User1006193418 posted

    Hi,

    As the following problem is much more based on ASP.NET instead of VB.NET itself, I suggest you post a new thread at the queue Getting Started or Web Forms.

    Thanks for your understanding.

    Best Regards,
    Shengqing Yang

    Tuesday, May 11, 2010 4:52 AM