Results 1 to 5 of 5

Thread: [RESOLVED] how to distict if is it a file or a directory (like explorer listview)

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2015
    Posts
    171

    Resolved [RESOLVED] how to distict if is it a file or a directory (like explorer listview)

    Hello,
    I'm using the followin code to trying to navigate folders and file; now I need to load the file if selected( so if it's a directory I explore it else, I load the file into stream), but I can't distinct if is clicked a file or a directory; I tryed some approtch like distict by the last character( if it's "" or not etc). There is better way?
    Code:
    Imports System.IO
    Imports System.IO.Path.PathSeparator
    Partial Public Class Form1
        Inherits Form
        Public Shared drives As String() = DriveInfo.GetDrives().Where(Function(drive) drive.IsReady).[Select](Function(drive) drive.Name).ToArray()
        Private _controller As DirectoryController
    
        Public Sub New()
            InitializeComponent()
            Dim imgList As ImageList = GetFolderImageList()
            InitializeListView(imgList)
            _controller = New DirectoryController(cmb1.SelectedItem)
            OpenDirectory("")
        End Sub
    
        Private Sub InitializeListView(ByVal imgList As ImageList)
            AddHandler lvExplorer.DoubleClick, New System.EventHandler(AddressOf Me.lvExplorer_DoubleClick)
            lvExplorer.SmallImageList = imgList
            lvExplorer.LargeImageList = imgList
        End Sub
    
        Private Shared Function GetFolderImageList() As ImageList
            Dim folderImg As Image = Image.FromFile("C:\Windows\WinSxS\amd64_microsoft-windows-printing-fdprint_31bf3856ad364e35_10.0.19041.1_none_0e211e3b24a05820\folder.ico")
            Dim imgList As ImageList = New ImageList()
            imgList.Images.Add(folderImg)
            Return imgList
        End Function
    
        Private Sub lvExplorer_DoubleClick(ByVal sender As Object, ByVal e As EventArgs)
            Dim path As String = lvExplorer.SelectedItems(0).Text
    
    
            OpenDirectory(path)
        End Sub
    
        Private Sub OpenDirectory(ByVal path As String)
            Try
                lvExplorer.Items.Clear()
                Dim newPath As String = _controller.AddDirectoryAndGetPath(path)
    
                ShowDirectoriesInListView(newPath)
    
                ShowFilesInListView(newPath)
    
            Catch ex As Exception
    
                MessageBox.Show(ex.Message)
    
    
            End Try
        End Sub
    
        Private Sub ShowDirectoriesInListView(ByVal path As String)
            Dim info As DirectoryInfo = New DirectoryInfo(path)
            Dim parent As DirectoryInfo = info.Parent
    
            If parent IsNot Nothing Then
                lvExplorer.Items.Add(New System.Windows.Forms.ListViewItem("...", 0))
            End If
    
            For Each dInfo As DirectoryInfo In info.GetDirectories()
                lvExplorer.Items.Add(New System.Windows.Forms.ListViewItem(dInfo.Name, 0))
            Next
        End Sub
        Private Sub ShowFilesInListView(ByVal path As String)
            Dim folderPath = path
            Dim folder As New DirectoryInfo(folderPath)
    
            lvExplorer.BeginUpdate()
    
            For Each file In folder.EnumerateFiles("*.*")
                lvExplorer.Items.Add(New ListViewItem({file.Name,
                                                      file.Extension,
                                                      (file.Length / (1024)).ToString("#,0.00 KB"),
                                                      file.LastWriteTime})) ' da correggere unità di misura
            Next
    
            lvExplorer.EndUpdate()
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            VistaFolderBrowserDialog1.ShowDialog()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For i = 0 To drives.Count - 1
                cmb1.Items.Add(drives(i))
            Next
        End Sub
    
        Private Sub cmb1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmb1.SelectedIndexChanged
            _controller = New DirectoryController(cmb1.SelectedItem)
            OpenDirectory("")
        End Sub
    
    
    End Class
    Class DirectoryController
        Public Shared _currentDirectory As String
    
        Public Sub New(ByVal beginDirectory As String)
            _currentDirectory = beginDirectory
        End Sub
    
        Public Function AddDirectoryAndGetPath(ByVal path As String) As String
            If path.Equals("...") Then
                Dim lastIndex As Integer = _currentDirectory.Length
    
                If _currentDirectory.EndsWith("\") Then
                    lastIndex = _currentDirectory.LastIndexOf("\")
                    _currentDirectory = _currentDirectory.Remove(lastIndex)
    
    
                End If
    
    
                lastIndex = _currentDirectory.LastIndexOf("\") + 1
                _currentDirectory = _currentDirectory.Remove(lastIndex)
            Else
                _currentDirectory = String.Format("{0}{1}{2}", _currentDirectory, path, "\")
            End If
    
            Return _currentDirectory
        End Function
    End Class

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: how to distict if is it a file or a directory (like explorer listview)

    You should be able to use https://docs.microsoft.com/en-us/dot...s?view=net-5.0 to check, if it returns true then it is a directory and not a file.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: how to distict if is it a file or a directory (like explorer listview)

    Why have you posted so much code? Surely you can narrow it down to just the code that's relevant to the issue. I came here to try to help but I'm not inclined to wade through all that obviously irrelevant code to try to work out where the issue might be when you should already know that much.

  4. #4
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: how to distict if is it a file or a directory (like explorer listview)

    Quote Originally Posted by PlausiblyDamp View Post
    You should be able to use https://docs.microsoft.com/en-us/dot...s?view=net-5.0 to check, if it returns true then it is a directory and not a file.
    Another alternative would be to store the DirInfo/FileInfo in the .Tag property of the newly created ListViewItem ... then you could check the TypeOf to take the appropriate action.

    Or heck, just set .Tag to "Dir" or "File" and act accordingly... That's the simple route. Using DirInfo/FileInfo though gives you access to more information about each item.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: how to distict if is it a file or a directory (like explorer listview)

    Both DirectoryInfo and FileInfo inherit FileSystemInfo. You can use the Attributes property to check whether it's a folder or not, e.g.
    vb.net Code:
    1. For Each fsi In New DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyDocuments).GetFileSystemInfos()
    2.     Dim isFolder = (fsi.Attributes And FileAttributes.Directory) = FileAttributes.Directory
    3.  
    4.     Console.WriteLine($"{fsi.FullName} {If(isFolder, "is", "is not")} a folder")
    5. Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width