Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all !

I am currently developing a file explorer application for windows mobile 5.0.
I have successfully put the system directories in my treeview control but I want to put icons on each folder and each file using system icons. And also how to create background thread in loading files. When I try to load files in \Windows\ directory, it takes 20 seconds to load all the files. I have tried creating thread for it but no changes at all. This is my code.

' Explorer Class
Imports System.Runtime.InteropServices
Imports System.IntPtr

Public Class Explorer
    Private Info As ArrayList
#Region "Enumeration"
    Private Enum Types
        File
        Folder
    End Enum
#End Region
#Region "Structure"
    Private Structure SFile
        Public FullName As String
        Public Name As String
        Public Size As String
        Public Modified As String
        Public Type As Types
    End Structure
#End Region
#Region "Function"
    Private Function FormatSize(ByVal size As Long) As String
        Dim sizes() As String = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}
        Dim formattedsize As Long = size
        Dim sizeIndex As Int32
        While formattedsize >= 1024 And sizeIndex < sizes.Length
            formattedsize /= 1024
            sizeIndex += 1
        End While
        Return Math.Round(formattedsize, 2).ToString & " " & sizes(sizeIndex)
    End Function
#End Region
#Region "Sub"
    Public Sub New()
    End Sub
    Public Sub LoadTree(ByVal nTree As TreeNode, ByVal Path As String)
        Dim ePath() As String = System.IO.Directory.GetDirectories("\" & Path.Trim("\"))
        For Each strPath In ePath
            nTree.Nodes.Add(strPath.Substring(strPath.LastIndexOf("\"), strPath.Length - strPath.LastIndexOf("\")).Trim("\"))
            Dim nNtree As TreeNode = nTree.Nodes(nTree.Nodes.Count - 1)
            Dim FullPath As String = nNtree.FullPath.Trim("\")
            FullPath = FullPath.Substring(FullPath.IndexOf("\"), FullPath.Length - FullPath.IndexOf("\"))
            LoadTree(nNtree, FullPath)
        Next
    End Sub
    Public Sub FillListView(ByVal _listView As ListView, ByVal Path As String)
        FillArrayWithFilesAndFolder(Info, Path)

        _listView.Clear()
        ' Set Columns
        _listView.Columns.Add("Name", _listView.Width / 3, HorizontalAlignment.Left)
        _listView.Columns.Add("Modified", 4 * _listView.Width / 15, HorizontalAlignment.Left)
        _listView.Columns.Add("Size", _listView.Width / 5, HorizontalAlignment.Right)
        _listView.Columns.Add("Type", _listView.Width / 5, HorizontalAlignment.Left)

        For Each ArrayItem As Object In Info
            Dim fullname As String = ""
            Dim lvitem As ListViewItem = New ListViewItem(DirectCast(ArrayItem, SFile).Name)
            lvitem.SubItems.Add(DirectCast(ArrayItem, SFile).Modified)
            If DirectCast(ArrayItem, SFile).Size = Nothing Then lvitem.SubItems.Add("") Else lvitem.SubItems.Add(FormatSize(CLng(DirectCast(ArrayItem, SFile).Size)))
            lvitem.SubItems.Add(If(DirectCast(ArrayItem, SFile).Type = Types.File, "File", "Folder"))
            _listView.Items.Add(lvitem)
        Next

    End Sub
    Private Sub FillArrayWithFilesAndFolder(ByRef InfoList As ArrayList, ByVal Path As String)
        InfoList = New ArrayList()
        InfoList.Clear()
        If System.IO.Directory.Exists(Path) Then
            Try
                Dim dr() As String = System.IO.Directory.GetDirectories(Path)
                For Each folder As String In dr
                    Dim FileInfo As SFile = New SFile
                    FileInfo.FullName = folder
                    FileInfo.Type = Types.Folder
                    FileInfo.Modified = System.IO.Directory.GetLastWriteTime(folder).ToString
                    FileInfo.Size = Nothing
                    FileInfo.Name = folder.Substring(folder.LastIndexOf("\"), folder.Length - folder.LastIndexOf("\")).Trim("\")
                    InfoList.Add(FileInfo)
                Next
                Dim fl() As String = System.IO.Directory.GetFiles(Path)
                For Each files As String In fl
                    Dim FileInfo As SFile = New SFile
                    FileInfo.FullName = files
                    FileInfo.Type = Types.File
                    FileInfo.Modified = System.IO.Directory.GetLastWriteTime(files).ToString
                    FileInfo.Name = files.Substring(files.LastIndexOf("\"), files.Length - files.LastIndexOf("\")).Trim("\")
                    Try
                        Using Fsize As System.IO.FileStream = System.IO.File.OpenRead(files)
                            FileInfo.Size = Fsize.Length.ToString
                        End Using
                    Catch ex As Exception
                        FileInfo.Size = "0"
                    End Try
                    InfoList.Add(FileInfo)
                Next
            Catch ex As Exception
            End Try
        End If
    End Sub
#End Region

End Class

' FORM Class
VB
Public Class frmMain
    Dim Moving As Boolean = False
    Dim nExplorer As Explorer
    Const SplitterWidth As Integer = 8
    Const Distance As Integer = 6
    Const MouseDistance As Integer = 5
    Const EdgeDistance As Integer = 10
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        nExplorer = New Explorer
        lvwPath.Capture = False
        eTree.Nodes.Clear()
        eTree.Nodes.Add("Window Delve")

        Dim nTree As TreeNode = eTree.Nodes(eTree.Nodes.Count - 1)
        nExplorer.LoadTree(nTree, "\")
        nExplorer.FillListView(lvwPath, "\")
        eTree.Nodes(0).Expand()
        picSplitter.Location = New Point(Me.Width / 2, 0)
        RelocateObj()
    End Sub
    Private Sub RelocateObj()
        picSplitter.Size = New Size(SplitterWidth, Me.Height - If(eHideToolBar.Checked, 0, stBar.Height))
        eTree.Size = New Size(picSplitter.Left, Me.Height - If(eHideToolBar.Checked, 0, stBar.Height))
        eTree.Location = New Point(0, 0)
        lvwPath.Location = New Point(picSplitter.Left + Distance, 0)
        lvwPath.Size = New Size(Me.Width - lvwPath.Left, Me.Height - If(eHideToolBar.Checked, 0, stBar.Height))
    End Sub

    Private Sub frmMain_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
        Moving = True
        picSplitter.Visible = True
        picSplitter.Left = e.X - MouseDistance
    End Sub
    Private Sub frmMain_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
        If Moving Then
            If picSplitter.Left - EdgeDistance > 0 Or picSplitter.Left + EdgeDistance < Me.Width Then
                picSplitter.Left = e.X - MouseDistance
                If eDrag.Checked Then RelocateObj()
            End If
        End If
    End Sub

    Private Sub frmMain_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
        Moving = False
        picSplitter.Visible = False
        If Not eDrag.Checked Then RelocateObj()
    End Sub
    Private Sub picSplitter_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picSplitter.Resize
        If Not Moving Then RelocateObj()
    End Sub
    Private Sub eTree_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles eTree.AfterSelect
        Cursor.Current = Cursors.WaitCursor
        stBar.Text = eTree.SelectedNode.Text
        Dim fullpath As String = eTree.SelectedNode.FullPath.Replace("Window Delve", "")
        nExplorer.FillListView(lvwPath, fullpath)
        Cursor.Current = Cursors.Default
    End Sub

    Private Sub eDrag_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eDrag.Click
        eDrag.Checked = Not eDrag.Checked
    End Sub

    Private Sub eHideToolBar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eHideToolBar.Click
        eHideToolBar.Checked = Not eHideToolBar.Checked
        stBar.Visible = Not eHideToolBar.Checked
        RelocateObj()
    End Sub

End Class

here is my program
http://www.4shared.com/file/kTylfHIg/WindowDelve.html[^]
Thank you!!! :)
Posted
Updated 2-Aug-11 20:40pm
v2

1 solution

Use a Background worker thread. For a sample, see BackgroundWorker Threads and Supporting Cancel[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900