Advertisement
hungcoder92

[DEVEXPRESS] Using TreeView tool as FTP File Manager application

Apr 23rd, 2022
2,086
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 10.31 KB | None | 0 0
  1. Imports System.Collections
  2. Imports System.ComponentModel
  3. Imports System.IO
  4. Imports System.Text
  5. Imports DevExpress.XtraTreeList.Nodes
  6. Imports FluentFTP
  7.  
  8. Partial Public Class FrmMain
  9.     Dim loadCustomPath As Boolean = True
  10.  
  11.     Dim ftpClient As FtpClient
  12.     Dim PATH_ROOT As String = "/"
  13.     Dim SERVER As String = "127.0.0.1" '192.168.0.78
  14.     Dim USERNAME As String = "user" 'admin
  15.     Dim PASSWORD As String = "123456" '123
  16.  
  17.  
  18.     Dim listMyImageCollection As List(Of MyImageCollection) = New List(Of MyImageCollection)
  19.  
  20.     Public Sub New()
  21.         InitializeComponent()
  22.         ftpClient = New FtpClient(SERVER, USERNAME, PASSWORD)
  23.         ftpClient.Connect()
  24.  
  25.     End Sub
  26.  
  27.     Private Sub FrmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  28.         tree_filemanager.DataSource = New Object()
  29.         tree_filemanager.StateImageList = ImageCollection
  30.         tree_filemanager.OptionsBehavior.Editable = False
  31.         tree_filemanager.OptionsView.EnableAppearanceEvenRow = True
  32.         tree_filemanager.OptionsView.ShowCheckBoxes = True
  33.  
  34.         'Dim a = GetListFileFolderFTP(PATH_ROOT)
  35.  
  36.     End Sub
  37.     Protected Overrides Sub OnShown(e As EventArgs)
  38.         tree_filemanager.ExpandToLevel(1)
  39.         MyBase.OnShown(e)
  40.     End Sub
  41.  
  42.     Private Function GetListFileFolderFTP(path As String) As List(Of FTPFileInfo)
  43.         Dim listData As New List(Of FTPFileInfo)
  44.         For Each item As FtpListItem In ftpClient.GetListing(path)
  45.  
  46.             If item.Type = FtpFileSystemObjectType.File Then
  47.                 Dim size As Long = 0
  48.                 Dim ftpFileInfo As New FTPFileInfo
  49.                 ftpFileInfo.FileName = item.FullName
  50.                 ftpFileInfo.Size = size
  51.                 ftpFileInfo.Type = "File"
  52.                 listData.Add(ftpFileInfo)
  53.             Else
  54.                 Dim ftpFileInfo As New FTPFileInfo
  55.                 ftpFileInfo.FileName = item.FullName
  56.                 ftpFileInfo.Size = 0
  57.                 ftpFileInfo.Type = "Folder"
  58.                 listData.Add(ftpFileInfo)
  59.             End If
  60.  
  61.  
  62.         Next
  63.         Return listData
  64.     End Function
  65.  
  66.     Private Sub tree_filemanager_VirtualTreeGetChildNodes(sender As Object, e As DevExpress.XtraTreeList.VirtualTreeGetChildNodesInfo) Handles tree_filemanager.VirtualTreeGetChildNodes
  67.         Dim current As Cursor = Cursor.Current
  68.         Cursor.Current = Cursors.WaitCursor
  69.         If loadCustomPath Then
  70.             e.Children = New String() {PATH_ROOT}
  71.             loadCustomPath = False
  72.         Else
  73.             Try
  74.                 Dim path As String = CStr(e.Node)
  75.  
  76.                 If Not IsFile(New DirectoryInfo(path)) Then
  77.                     Dim listFileAndFolder = GetListFileFolderFTP(path)
  78.                     Dim dirs() As String = listFileAndFolder.Where(Function(s) s.Type = "Folder").Select(Function(t) t.FileName).ToArray()
  79.                     Dim files() As String = listFileAndFolder.Where(Function(s) s.Type = "File").Select(Function(t) t.FileName).ToArray()
  80.                     Dim arr(dirs.Length + files.Length - 1) As String
  81.                     dirs.CopyTo(arr, 0)
  82.                     files.CopyTo(arr, dirs.Length)
  83.                     e.Children = arr
  84.                 Else
  85.                     Dim ftpFileInfo = CType(e.Node, FTPFileInfo)
  86.                     Dim listFileAndFolder = GetListFileFolderFTP(ftpFileInfo.FileName)
  87.                     If ftpFileInfo.Type.Equals("Folder") Then
  88.                         e.Children = listFileAndFolder
  89.                     Else
  90.                         e.Children = New Object() {}
  91.                     End If
  92.                 End If
  93.  
  94.  
  95.  
  96.                 'If ftpClient.DirectoryExists(path) Then
  97.                 '    Dim dirs() As String = listFileAndFolder.Where(Function(s) s.Type = "Folder").Select(Function(t) t.FileName).ToArray()
  98.                 '    Dim files() As String = listFileAndFolder.Where(Function(s) s.Type = "File").Select(Function(t) t.FileName).ToArray()
  99.                 '    Dim arr(dirs.Length + files.Length - 1) As String
  100.                 '    dirs.CopyTo(arr, 0)
  101.                 '    files.CopyTo(arr, dirs.Length)
  102.                 '    e.Children = arr
  103.                 'Else
  104.                 '    e.Children = New Object() {}
  105.                 'End If
  106.             Catch
  107.                 e.Children = New Object() {}
  108.             End Try
  109.         End If
  110.         Cursor.Current = current
  111.     End Sub
  112.     Private Sub tree_filemanager_VirtualTreeGetCellValue(sender As Object, e As DevExpress.XtraTreeList.VirtualTreeGetCellValueInfo) Handles tree_filemanager.VirtualTreeGetCellValue
  113.         Dim di = e.Node
  114.  
  115.         If e.Column Is colName Then
  116.             If di Is PATH_ROOT Then
  117.                 e.CellData = di.ToString()
  118.             Else
  119.                 'Dim ftpFileInfo = CType(di, FTPFileInfo)
  120.                 e.CellData = New DirectoryInfo(di.ToString()).Name
  121.             End If
  122.         End If
  123.         If e.Column Is colType Then
  124.             If di Is PATH_ROOT Then
  125.                 e.CellData = "Root"
  126.                 Return
  127.             End If
  128.  
  129.             If Not IsFile(New DirectoryInfo(di.ToString())) Then
  130.                 e.CellData = "Folder"
  131.             Else
  132.                 e.CellData = "File"
  133.             End If
  134.         End If
  135.         If e.Column Is colSize Then
  136.  
  137.             If IsFile(New DirectoryInfo(di.ToString())) Then
  138.                 e.CellData = ftpClient.GetFileSize(di.ToString())
  139.             Else
  140.                 e.CellData = Nothing
  141.             End If
  142.  
  143.         End If
  144.     End Sub
  145.  
  146.     Private Function IsFile(ByVal info As DirectoryInfo) As Boolean
  147.         Try
  148.             Return info.Extension.Length > 0
  149.         Catch
  150.             Return False
  151.         End Try
  152.     End Function
  153.     Dim index As Integer = 3
  154.     Private Sub tree_filemanager_GetStateImage(sender As Object, e As DevExpress.XtraTreeList.GetStateImageEventArgs) Handles tree_filemanager.GetStateImage
  155.         If e.Node.GetDisplayText("Type") = "Folder" Then
  156.             e.NodeImageIndex = If(e.Node.Expanded, 1, 0)
  157.         ElseIf e.Node.GetDisplayText("Type") = "File" Then
  158.             Dim b = e.Node.GetDisplayText("Name")
  159.             Dim existFileExtension = listMyImageCollection.Any(Function(x) x.FileExtension = Path.GetExtension(e.Node.GetDisplayText("Name")))
  160.             If Not existFileExtension Then
  161.                 ImageCollection.AddImage(FileIconFromExtensionHelper.FileIconLoader.GetFileIcon(e.Node.GetDisplayText("Name"), False).ToBitmap())
  162.                 Dim myImage = New MyImageCollection()
  163.                 myImage.FileExtension = Path.GetExtension(e.Node.GetDisplayText("Name"))
  164.                 index = index + 1
  165.                 myImage.Index = index
  166.                 listMyImageCollection.Add(myImage)
  167.             End If
  168.             Dim myIndex = listMyImageCollection.Where(Function(x) x.FileExtension = Path.GetExtension(e.Node.GetDisplayText("Name"))).FirstOrDefault().Index
  169.  
  170.             e.NodeImageIndex = myIndex
  171.         Else
  172.             e.NodeImageIndex = 3
  173.         End If
  174.     End Sub
  175.  
  176.     Private Sub tree_filemanager_CustomDrawNodeCell(sender As Object, e As DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs) Handles tree_filemanager.CustomDrawNodeCell
  177.         If e.Column Is Me.colSize Then
  178.             If e.Node.GetDisplayText("Type") = "File" Then
  179.                 e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far
  180.                 Dim size As Int64 = Convert.ToInt64(e.Node.GetValue("Size"))
  181.                 e.CellText = If(size = 0, "0 Byte", FileHelper.ToPrettySize(size))
  182.             Else
  183.                 e.CellText = String.Format("<{0}>", e.Node.GetDisplayText("Type"))
  184.             End If
  185.         End If
  186.  
  187.         If e.Column Is Me.colName Then
  188.             If e.Node.GetDisplayText("Type") = "File" Then
  189.                 e.Appearance.Font = New Font(e.Appearance.Font, FontStyle.Bold)
  190.             End If
  191.         End If
  192.     End Sub
  193.  
  194.     Private Sub tree_filemanager_AfterCheckNode(sender As Object, e As DevExpress.XtraTreeList.NodeEventArgs) Handles tree_filemanager.AfterCheckNode
  195.         If e.Node.CheckState = CheckState.Checked Then
  196.             If e.Node.GetValue(1) Is "File" Then
  197.                 MsgBox("Checked: File - " & FullNameByNode(e.Node, 0))
  198.             End If
  199.  
  200.             If e.Node.GetValue(1) Is "Folder" Or e.Node.GetValue(1) Is "Drive" Then
  201.                 MsgBox("Checked: Folder - " & FullNameByNode(e.Node, 0))
  202.             End If
  203.         End If
  204.         If e.Node.CheckState = CheckState.Unchecked Then
  205.  
  206.             If e.Node.GetValue(1) Is "File" Then
  207.                 MsgBox("Unchecked: File - " & FullNameByNode(e.Node, 0))
  208.             End If
  209.  
  210.             If e.Node.GetValue(1) Is "Folder" Or e.Node.GetValue(1) Is "Drive" Then
  211.                 MsgBox("Unchecked: Folder - " & FullNameByNode(e.Node, 0))
  212.             End If
  213.         End If
  214.     End Sub
  215.  
  216.     Private Function FullNameByNode(ByVal node As TreeListNode, ByVal columnId As Integer) As String
  217.         Dim ret As String = node.GetValue(columnId).ToString()
  218.         Do While node.ParentNode IsNot Nothing
  219.             node = node.ParentNode
  220.             ret = String.Concat(node.GetValue(columnId), "\").TrimEnd("\"c) & "\" & ret
  221.         Loop
  222.         Return ret
  223.     End Function
  224.  
  225.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  226.  
  227.         'Dim nodes As List(Of TreeListNode) = tree_filemanager.GetNodeList()
  228.         tree_filemanager.ExpandAll()
  229.         For Each node As TreeListNode In tree_filemanager.GetNodeList()
  230.             'MsgBox(node.GetDisplayText(0))
  231.             'node.SetValue("duongdan", FullNameByNode(node, 1)) ' Lấy nguyên đường dẫn
  232.             node.SetValue("duongdan", FullNameByNode(node, 0))
  233.             node.SetValue("tenfile", node.GetDisplayText("Name"))
  234.             node.SetValue("id", 1)
  235.             node.SetValue("daky", 0)
  236.             node.SetValue("nguoinhan", "")
  237.             node.SetValue("lantai", 0)
  238.             node.SetValue("nguoitao", "Tự động")
  239.             node.SetValue("ngaytao", Now())
  240.             node.SetValue("nguoisua", "")
  241.         Next
  242.  
  243.     End Sub
  244.  
  245.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  246.         'tree_filemanager.ExpandToLevel(1)
  247.         loadCustomPath = True
  248.         tree_filemanager.DataSource = New Object()
  249.         tree_filemanager.ExpandToLevel(1)
  250.     End Sub
  251. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement