NEWS

[DEVEXPRESS] Sử dụng công cụ TreeView làm ứng dụng quản lý FTP File Manager

[DEVEXPRESS] Sử dụng công cụ TreeView làm ứng dụng quản lý FTP File Manager
Đăng bởi: Thảo Meo - Lượt xem: 3971 08:28:41, 25/03/2022C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình chia sẻ các bạn sử dụng công cụ TreeView trên Devexpress làm công cụ Quản lý FTP File Manager.

[DEVEXPRESS] FTP File Manager Winform

Với ứng dụng code dưới đây, các bạn có thể dễ dàng tải thông tin file và folder trên ftp về phần mềm.

Giao diện demo ứng dụng FTP File Manager:

file_ftp_manager

Ở App này hiển thị các thông tin: Tên file or folder, kiểu dữ liệu, kích thước file.

Khi các bạn click vào từng file hoặc folder các bạn có thể lấy được Full đường dẫn.

Source code VB.NET:

Imports System.Collections
Imports System.ComponentModel
Imports System.IO
Imports System.Text
Imports DevExpress.XtraTreeList.Nodes
Imports FluentFTP

Partial Public Class FrmMain
    Dim loadCustomPath As Boolean = True

    Dim ftpClient As FtpClient
    Dim PATH_ROOT As String = "/"
    Dim SERVER As String = "127.0.0.1" '192.168.0.78
    Dim USERNAME As String = "user" 'admin
    Dim PASSWORD As String = "123456" '123


    Dim listMyImageCollection As List(Of MyImageCollection) = New List(Of MyImageCollection)

    Public Sub New()
        InitializeComponent()
        ftpClient = New FtpClient(SERVER, USERNAME, PASSWORD)
        ftpClient.Connect()

    End Sub

    Private Sub FrmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        tree_filemanager.DataSource = New Object()
        tree_filemanager.StateImageList = ImageCollection
        tree_filemanager.OptionsBehavior.Editable = False
        tree_filemanager.OptionsView.EnableAppearanceEvenRow = True
        tree_filemanager.OptionsView.ShowCheckBoxes = True

        'Dim a = GetListFileFolderFTP(PATH_ROOT)

    End Sub
    Protected Overrides Sub OnShown(e As EventArgs)
        tree_filemanager.ExpandToLevel(1)
        MyBase.OnShown(e)
    End Sub

    Private Function GetListFileFolderFTP(path As String) As List(Of FTPFileInfo)
        Dim listData As New List(Of FTPFileInfo)
        For Each item As FtpListItem In ftpClient.GetListing(path)

            If item.Type = FtpFileSystemObjectType.File Then
                Dim size As Long = 0
                Dim ftpFileInfo As New FTPFileInfo
                ftpFileInfo.FileName = item.FullName
                ftpFileInfo.Size = size
                ftpFileInfo.Type = "File"
                listData.Add(ftpFileInfo)
            Else
                Dim ftpFileInfo As New FTPFileInfo
                ftpFileInfo.FileName = item.FullName
                ftpFileInfo.Size = 0
                ftpFileInfo.Type = "Folder"
                listData.Add(ftpFileInfo)
            End If


        Next
        Return listData
    End Function

    Private Sub tree_filemanager_VirtualTreeGetChildNodes(sender As Object, e As DevExpress.XtraTreeList.VirtualTreeGetChildNodesInfo) Handles tree_filemanager.VirtualTreeGetChildNodes
        Dim current As Cursor = Cursor.Current
        Cursor.Current = Cursors.WaitCursor
        If loadCustomPath Then
            e.Children = New String() {PATH_ROOT}
            loadCustomPath = False
        Else
            Try
                Dim path As String = CStr(e.Node)

                If Not IsFile(New DirectoryInfo(path)) Then
                    Dim listFileAndFolder = GetListFileFolderFTP(path)
                    Dim dirs() As String = listFileAndFolder.Where(Function(s) s.Type = "Folder").Select(Function(t) t.FileName).ToArray()
                    Dim files() As String = listFileAndFolder.Where(Function(s) s.Type = "File").Select(Function(t) t.FileName).ToArray()
                    Dim arr(dirs.Length + files.Length - 1) As String
                    dirs.CopyTo(arr, 0)
                    files.CopyTo(arr, dirs.Length)
                    e.Children = arr
                Else
                    Dim ftpFileInfo = CType(e.Node, FTPFileInfo)
                    Dim listFileAndFolder = GetListFileFolderFTP(ftpFileInfo.FileName)
                    If ftpFileInfo.Type.Equals("Folder") Then
                        e.Children = listFileAndFolder
                    Else
                        e.Children = New Object() {}
                    End If
                End If



                'If ftpClient.DirectoryExists(path) Then
                '    Dim dirs() As String = listFileAndFolder.Where(Function(s) s.Type = "Folder").Select(Function(t) t.FileName).ToArray()
                '    Dim files() As String = listFileAndFolder.Where(Function(s) s.Type = "File").Select(Function(t) t.FileName).ToArray()
                '    Dim arr(dirs.Length + files.Length - 1) As String
                '    dirs.CopyTo(arr, 0)
                '    files.CopyTo(arr, dirs.Length)
                '    e.Children = arr
                'Else
                '    e.Children = New Object() {}
                'End If
            Catch
                e.Children = New Object() {}
            End Try
        End If
        Cursor.Current = current
    End Sub
    Private Sub tree_filemanager_VirtualTreeGetCellValue(sender As Object, e As DevExpress.XtraTreeList.VirtualTreeGetCellValueInfo) Handles tree_filemanager.VirtualTreeGetCellValue
        Dim di = e.Node

        If e.Column Is colName Then
            If di Is PATH_ROOT Then
                e.CellData = di.ToString()
            Else
                'Dim ftpFileInfo = CType(di, FTPFileInfo)
                e.CellData = New DirectoryInfo(di.ToString()).Name
            End If
        End If
        If e.Column Is colType Then
            If di Is PATH_ROOT Then
                e.CellData = "Root"
                Return
            End If

            If Not IsFile(New DirectoryInfo(di.ToString())) Then
                e.CellData = "Folder"
            Else
                e.CellData = "File"
            End If
        End If
        If e.Column Is colSize Then

            If IsFile(New DirectoryInfo(di.ToString())) Then
                e.CellData = ftpClient.GetFileSize(di.ToString())
            Else
                e.CellData = Nothing
            End If

        End If
    End Sub

    Private Function IsFile(ByVal info As DirectoryInfo) As Boolean
        Try
            Return info.Extension.Length > 0
        Catch
            Return False
        End Try
    End Function
    Dim index As Integer = 3
    Private Sub tree_filemanager_GetStateImage(sender As Object, e As DevExpress.XtraTreeList.GetStateImageEventArgs) Handles tree_filemanager.GetStateImage
        If e.Node.GetDisplayText("Type") = "Folder" Then
            e.NodeImageIndex = If(e.Node.Expanded, 1, 0)
        ElseIf e.Node.GetDisplayText("Type") = "File" Then
            Dim b = e.Node.GetDisplayText("Name")
            Dim existFileExtension = listMyImageCollection.Any(Function(x) x.FileExtension = Path.GetExtension(e.Node.GetDisplayText("Name")))
            If Not existFileExtension Then
                ImageCollection.AddImage(FileIconFromExtensionHelper.FileIconLoader.GetFileIcon(e.Node.GetDisplayText("Name"), False).ToBitmap())
                Dim myImage = New MyImageCollection()
                myImage.FileExtension = Path.GetExtension(e.Node.GetDisplayText("Name"))
                index = index + 1
                myImage.Index = index
                listMyImageCollection.Add(myImage)
            End If
            Dim myIndex = listMyImageCollection.Where(Function(x) x.FileExtension = Path.GetExtension(e.Node.GetDisplayText("Name"))).FirstOrDefault().Index

            e.NodeImageIndex = myIndex
        Else
            e.NodeImageIndex = 3
        End If
    End Sub

    Private Sub tree_filemanager_CustomDrawNodeCell(sender As Object, e As DevExpress.XtraTreeList.CustomDrawNodeCellEventArgs) Handles tree_filemanager.CustomDrawNodeCell
        If e.Column Is Me.colSize Then
            If e.Node.GetDisplayText("Type") = "File" Then
                e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far
                Dim size As Int64 = Convert.ToInt64(e.Node.GetValue("Size"))
                e.CellText = If(size = 0, "0 Byte", FileHelper.ToPrettySize(size))
            Else
                e.CellText = String.Format("<{0}>", e.Node.GetDisplayText("Type"))
            End If
        End If

        If e.Column Is Me.colName Then
            If e.Node.GetDisplayText("Type") = "File" Then
                e.Appearance.Font = New Font(e.Appearance.Font, FontStyle.Bold)
            End If
        End If
    End Sub

    Private Sub tree_filemanager_AfterCheckNode(sender As Object, e As DevExpress.XtraTreeList.NodeEventArgs) Handles tree_filemanager.AfterCheckNode
        If e.Node.CheckState = CheckState.Checked Then
            If e.Node.GetValue(1) Is "File" Then
                MsgBox("Checked: File - " & FullNameByNode(e.Node, 0))
            End If

            If e.Node.GetValue(1) Is "Folder" Or e.Node.GetValue(1) Is "Drive" Then
                MsgBox("Checked: Folder - " & FullNameByNode(e.Node, 0))
            End If
        End If
        If e.Node.CheckState = CheckState.Unchecked Then

            If e.Node.GetValue(1) Is "File" Then
                MsgBox("Unchecked: File - " & FullNameByNode(e.Node, 0))
            End If

            If e.Node.GetValue(1) Is "Folder" Or e.Node.GetValue(1) Is "Drive" Then
                MsgBox("Unchecked: Folder - " & FullNameByNode(e.Node, 0))
            End If
        End If
    End Sub

    Private Function FullNameByNode(ByVal node As TreeListNode, ByVal columnId As Integer) As String
        Dim ret As String = node.GetValue(columnId).ToString()
        Do While node.ParentNode IsNot Nothing
            node = node.ParentNode
            ret = String.Concat(node.GetValue(columnId), "\").TrimEnd("\"c) & "\" & ret
        Loop
        Return ret
    End Function

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        'Dim nodes As List(Of TreeListNode) = tree_filemanager.GetNodeList()
        tree_filemanager.ExpandAll()
        For Each node As TreeListNode In tree_filemanager.GetNodeList()
            'MsgBox(node.GetDisplayText(0))
            'node.SetValue("duongdan", FullNameByNode(node, 1)) ' Lấy nguyên đường dẫn
            node.SetValue("duongdan", FullNameByNode(node, 0))
            node.SetValue("tenfile", node.GetDisplayText("Name"))
            node.SetValue("id", 1)
            node.SetValue("daky", 0)
            node.SetValue("nguoinhan", "")
            node.SetValue("lantai", 0)
            node.SetValue("nguoitao", "Tự động")
            node.SetValue("ngaytao", Now())
            node.SetValue("nguoisua", "")
        Next

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        'tree_filemanager.ExpandToLevel(1)
        loadCustomPath = True
        tree_filemanager.DataSource = New Object()
        tree_filemanager.ExpandToLevel(1)
    End Sub
End Class

Ở bài viết này, mình sử dụng thư viện Fluent FTP để làm việc với FTP.

Các bạn, có thể tìm hiểu thêm về thư viện Fluent FTP ở đây.

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Sử dụng công cụ TreeView làm ứng dụng quản lý FTP File Manager
Đăng bởi: Thảo Meo - Lượt xem: 3971 08:28:41, 25/03/2022C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.

Verified Website

See Report