-
Jun 21st, 2021, 07:55 AM
#1
Thread Starter
Addicted Member
[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
-
Jun 21st, 2021, 08:52 AM
#2
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.
-
Jun 21st, 2021, 09:20 AM
#3
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.
-
Jun 21st, 2021, 09:24 AM
#4
Re: how to distict if is it a file or a directory (like explorer listview)
Originally Posted by PlausiblyDamp
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
-
Jun 21st, 2021, 09:56 AM
#5
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:
For Each fsi In New DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyDocuments).GetFileSystemInfos()
Dim isFolder = (fsi.Attributes And FileAttributes.Directory) = FileAttributes.Directory
Console.WriteLine($"{fsi.FullName} {If(isFolder, "is", "is not")} a folder")
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|