Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey there every one for the past few months i have been building a file server with chat, now i dont like to ask much so i have work most of the code out from then net.

What it has:

User List
Login to server(.xml file server side)
has some admin cmd's
download files
chat. ect ect

all that works well so far but what i would like to do is add the files and folders to a tree view because atm i have it running from a list view and combo box but one can only change down one dir( i know why) so im thinking if i move it over to a tree view i can stor the full path and have icons and all that cool stuff.

things i know:

threading(a lil bit to get me going)
network streams
console app(coz it started like that)

things i dont know:

threading with a tree view( i cant get it to work right).
loading files from strings in to root nodes and setting up the tree veiw to take a file\folder list(with stored fullpath).

it may seem like a lot but i cant find any thing on listing files\folders on a server in a tree view.

im sorry for the spelling i know its fail but what can ya do :)
any way ty for your time and i would love a lil help.



the server is doing this:
VB
Public Sub DisplayTree(ByVal DirTree As String, ByVal Client As tcpConnection)
        Dim NameDir As IO.DirectoryInfo


        'FIrst Folder and File list
        If IO.Directory.Exists(DirTree) Then
            For Each MyDir As String In IO.Directory.GetDirectories(DirTree)
                NameDir = New IO.DirectoryInfo(MyDir)
                SendOnlineUsersToCLIENT(Client, 87, NameDir.Name & "$", "" + "$", NameDir.Name + "$")

                If IO.Directory.Exists(NameDir.ToString) Then
                    For Each MyDir1 As String In IO.Directory.GetFiles(NameDir.ToString)
                        NameDir = New IO.DirectoryInfo(MyDir1)
                        SendOnlineUsersToCLIENT(Client, 87, NameDir.Name & "$", "" + "$", NameDir.Name + "$")
                    Next
                End If
                If IO.Directory.Exists(MyDir) Then
                    Call DisplayTree(MyDir, Client)
                End If
            Next


            For Each MyDir As String In IO.Directory.GetFiles(DirTree)
                NameDir = New IO.DirectoryInfo(MyDir)
                SendOnlineUsersToCLIENT(Client, 87, NameDir.Name & "$", "" + "$", NameDir.Name + "$")

            Next

        End If
    End Sub



client is doing this :
VB
Private Sub client_ChatReceived(ByVal Sender As tcpConnection, ByVal msgTag As String, _
   ByVal mstream2 As String, ByVal mstream3 As String, ByVal mstream As String) Handles client.Chat

        Select Case msgTag
            Case Requests.MSG
                UpdateStatus4("<Global>" & " " & mstream3 & ": " & cryptout(mstream, mstream))

            Case Requests.ServerMSG
                UpdateStatus4("<Server Msg>" & " " & mstream3 & " " & mstream)

            Case Requests.Tell
                UpdateStatus4("<PM>" & " " & mstream3 & ": " & cryptout(mstream, mstream))

            Case Requests.ONLINEUSERS
                Dim dfdsf = frmMain.TreeView1.Nodes.ContainsKey(mstream2)
                If dfdsf = False Then
                    UpdateStatus78(mstream2)
                End If
            Case Requests.loginFail
                UpdateStatus4("<User Log>" & " " & mstream3 & " " & mstream)

            Case Requests.OFFLINEUSERS
                Dim dsads As New TreeNode
                dsads.Name = mstream3
                frmMain.TreeView1.Nodes.RemoveByKey(mstream3)
                UpdateStatus4("<User Log>" & " " & mstream3 & " " & mstream)

            Case 101
                UpdateStatus4(mstream2 & " " & mstream2 & ": " & mstream)
            Case 87


                UpdateStatus782(mstream2)

        End Select
    End Sub



and case 87 is calling this sorry to post the hole block but i figerd it would help:

VB
Public Sub UpdateStatus782(ByVal t As String)

      Try
          frmMain.TreeView2.Nodes.Add(t, t, 4, 4)
      Catch e As Exception

          If frmMain.TreeView2.InvokeRequired Then
              Dim str_text As String = CStr(t)
              Dim d As New Node(AddressOf UpdateStatus782)
              frmMain.TreeView2.Invoke(d, New String() {str_text})

          End If
      End Try
  End Sub
Posted
Updated 8-Nov-10 3:05am
v4
Comments
-~-Tiba-~- 2 8-Nov-10 7:19am    
ill upload the code if i have to but you will all LOL!! at my bad codeing
fjdiewornncalwe 8-Nov-10 9:26am    
If you are asking a serious question, no one will "LOL" your coding. We all started somewhere and ALL developers have code they are not proud of. Chin up, if you want to learn, let the good folks here help.

1 solution

ok so i found a way to do what i needed and that was:

The server loads files and folders into a Treeview it then saves the tree as an xml file and sends it to the clients at logon.
i have added the code i used to solve this i hope it helps.

ps. if the way im doing it is wrong then plz let me know ty

code that the server runs to make the treeveiw and save the xml:

VB
Public Sub kcTree2XML(ByVal XMLFilePath As String, ByVal kcTreeview As TreeView)
      Dim XMLDoc As New Xml.XmlDocument
      Dim RootNode As TreeNode
      Dim NewXMLNode As Xml.XmlNode
      Dim XMLAttribute As Xml.XmlAttribute

      XMLDoc.LoadXml(("<?xml version='1.0' ?>" & _
      "<XMLTreeView>" & _
      "</XMLTreeView>"))


      For Each RootNode In kcTreeview.Nodes
          NewXMLNode = XMLDoc.CreateNode(Xml.XmlNodeType.Element, "Node", "Node", "")
          XMLAttribute = XMLDoc.CreateAttribute("name")
          XMLAttribute.Value = RootNode.Text

          NewXMLNode.Attributes.Append(XMLAttribute)
          XMLDoc.DocumentElement.AppendChild(NewXMLNode)

          SaveChildNodes(NewXMLNode, RootNode, XMLDoc)
      Next

      XMLDoc.Save(XMLFilePath)

  End Sub

  Private Sub SaveChildNodes(ByVal XMLNode As Xml.XmlNode, ByVal ParentNode As TreeNode, ByVal XMLDoc As Xml.XmlDocument)
      Dim ChildNode As TreeNode
      Dim NewXMLNode As Xml.XmlNode
      Dim XMLAttribute As Xml.XmlAttribute

      For Each ChildNode In ParentNode.Nodes
          NewXMLNode = XMLDoc.CreateNode(Xml.XmlNodeType.Element, "Node", "Node", "")
          XMLAttribute = XMLDoc.CreateAttribute("name")
          XMLAttribute.Value = ChildNode.Text

          NewXMLNode.Attributes.Append(XMLAttribute)
          XMLNode.AppendChild(NewXMLNode)

          SaveChildNodes(NewXMLNode, ChildNode, XMLDoc)
      Next

  End Sub



  Public Sub DisplayTree(ByVal DirTree As String, ByVal stnode As TreeNode)
      Dim NameDir As IO.DirectoryInfo
      Dim MyNode, MyNode1 As TreeNode

      'FIrst Folder and File list
      If IO.Directory.Exists(DirTree) Then
          For Each MyDir As String In IO.Directory.GetDirectories(DirTree)
              NameDir = New IO.DirectoryInfo(mydir)
              ' AddIcon(NameDir.ToString)
              'MyNode = TrFoldList.Nodes.Add(NameDir.Name)
              MyNode = stnode.Nodes.Add(NameDir.Name)
              MyNode.ImageIndex = iconIn
              MyNode.SelectedImageIndex = iconIn
              If IO.Directory.Exists(NameDir.ToString) Then
                  For Each MyDir1 As String In IO.Directory.GetFiles(NameDir.ToString)
                      NameDir = New IO.DirectoryInfo(MyDir1)
                      ' AddIcon(NameDir.ToString)
                      MyNode1 = MyNode.Nodes.Add(NameDir.Name)
                      MyNode1.Tag = MyDir1
                      MyNode1.ImageIndex = iconIn
                      MyNode1.SelectedImageIndex = iconIn
                  Next
              End If
              If IO.Directory.Exists(MyDir) Then
                  Call DisplayTree(MyDir, MyNode)
              End If
          Next

          Dim MyNode2 As TreeNode
          For Each MyDir As String In IO.Directory.GetFiles(DirTree)
              NameDir = New IO.DirectoryInfo(MyDir)
              ' AddIcon(NameDir.ToString)
              MyNode2 = stnode.Nodes.Add(NameDir.Name)
              MyNode2.Tag = MyDir
              MyNode2.ImageIndex = iconIn
              MyNode2.SelectedImageIndex = iconIn

          Next

      End If
  End Sub







The client loads the xml and adds an icon to the node.
(the clsicon class is from devX same with the tree2XML function and XML2tree fucntion)

code to load the xml:


VB
Dim iconIn As Integer
   Private nIndex = 2



   Public Function kcXML2Tree(ByVal XMLFilepath As String, ByVal kcTreeview As TreeView)
       Dim XMLDoc As New Xml.XmlDocument
       Dim XMLNode As Xml.XmlNode
       Dim ParentNode As TreeNode

       If kcTreeview Is Nothing Then
           Throw New ArgumentNullException("TreeView")
       End If

       Try
           XMLDoc.Load(XMLFilepath)
       Catch e As Exception
           Throw New Exception(e.Message)
       End Try

       For Each XMLNode In XMLDoc.DocumentElement.ChildNodes
           AddIcon(XMLNode.Attributes(0).Value)
           kcTreeview.ImageList = frmMain.ImageList4
           ParentNode = kcTreeview.Nodes.Add(XMLNode.Attributes(0).Value, XMLNode.Attributes(0).Value)


           If XMLNode.ChildNodes.Count > 0 Then
               AddChildNodes(XMLNode, ParentNode)
           End If
       Next
   End Function

   Public Sub AddIcon(ByVal Namedir As String)
       Dim Test As New clsIcon()
       Dim iconimg = clsIcon.GetDefaultIcon(Namedir, clsIcon.IconSize.SmallIcon)

       frmMain.ImageList4.Images.Add(iconimg)
       'Add icon to
       'imageList.
       iconIn = nIndex
       'icon to listview.
       nIndex = nIndex + 1
   End Sub

   Private Sub AddChildNodes(ByVal XMLNode As Xml.XmlNode, ByVal ParentNode As TreeNode)

       Dim ChildXMLNode As Xml.XmlNode
       Dim NewNode As TreeNode

       For Each ChildXMLNode In XMLNode.ChildNodes

           AddIcon(ChildXMLNode.Attributes(0).Value)

           NewNode = ParentNode.Nodes.Add(ChildXMLNode.Attributes(0).Value, ChildXMLNode.Attributes(0).Value, iconIn, iconIn)
           NewNode.Parent.ImageIndex = 1
           NewNode.Parent.SelectedImageIndex = 1

           If ChildXMLNode.ChildNodes.Count > 0 Then
               AddChildNodes(ChildXMLNode, NewNode)
           End If
       Next

   End Sub





and now for the system icon class but it does not work with folders so i had to add an icon for that to the image list



'=====================================================================================
'  clsIcon
'  class to work with icons
'=====================================================================================
'  Created By: Marc Cramer
'  Published Date: 12/31/2002
'  Legal Copyright: Marc Cramer © 12/31/2002
'=====================================================================================
'  Adapted From...
'  Author: spotchannel (spotchannel@hotmail.com)
'  Website: forum post at http://www.devcity.net/forums/topic.asp?tid=7422
'=====================================================================================
Imports System
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Windows.Forms
Public Class clsIcon
    '=====================================================================================
    ' Enumerations
    '=====================================================================================
    <Flags()> Private Enum SHGFI
        SmallIcon = &H1
        LargeIcon = &H0
        Icon = &H100
        DisplayName = &H200
        Typename = &H400
        SysIconIndex = &H4000
        UseFileAttributes = &H10
    End Enum
    Public Enum IconSize
        SmallIcon = 1
        LargeIcon = 0
    End Enum
    '=====================================================================================
    ' Structures
    '=====================================================================================
    <StructLayout(LayoutKind.Sequential)> _
    Private Structure SHFILEINFO
        Public hIcon As IntPtr
        Public iIcon As Integer
        Public dwAttributes As Integer
        <MarshalAs(UnmanagedType.LPStr, SizeConst:=260)> Public szDisplayName As String
        <MarshalAs(UnmanagedType.LPStr, SizeConst:=80)> Public szTypeName As String
        Public Sub New(ByVal B As Boolean)
            hIcon = IntPtr.Zero
            iIcon = 0
            dwAttributes = 0
            szDisplayName = vbNullString
            szTypeName = vbNullString
        End Sub
    End Structure
    '=====================================================================================
    ' API Calls
    '=====================================================================================
    Private Declare Auto Function SHGetFileInfo Lib "shell32" (ByVal pszPath As String, ByVal dwFileAttributes As Integer, ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, ByVal uFlagsn As SHGFI) As Integer
    '=====================================================================================
    ' Functions and Procedures...
    '=====================================================================================
    Public Shared Function GetDefaultIcon(ByVal Path As String, Optional ByVal IconSize As IconSize = IconSize.SmallIcon, Optional ByVal SaveIconPath As String = "") As Icon
        Dim info As New SHFILEINFO(True)
        Dim cbSizeInfo As Integer = Marshal.SizeOf(info)
        Dim flags As SHGFI = SHGFI.Icon Or SHGFI.UseFileAttributes
        flags = flags + IconSize
        SHGetFileInfo(Path, 256, info, cbSizeInfo, flags)
        GetDefaultIcon = Icon.FromHandle(info.hIcon)
        If SaveIconPath <> "" Then
            Dim FileStream As New IO.FileStream(SaveIconPath, IO.FileMode.Create)
            GetDefaultIcon.Save(FileStream)
            FileStream.Close()
        End If
    End Function      'GetDefaultIcon(ByVal Path As String, Optional ByVal IconSize As IconSize = IconSize.SmallIcon, Optional ByVal SaveIconPath As String = "") As Icon
    '=====================================================================================
    Public Shared Function ImageToIcon(ByVal SourceImage As Image) As Icon
        ' converts an image into an icon
        Dim TempBitmap As New Bitmap(SourceImage)
        ImageToIcon = Icon.FromHandle(TempBitmap.GetHicon())
        TempBitmap.Dispose()
    End Function      'ImageToIcon(ByVal SourceImage As Image) As Icon
    '=====================================================================================

   
End Class
 
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