Results 1 to 3 of 3

Thread: cant get my file browser to show correct file type - check out the screenshot

  1. #1

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    49

    cant get my file browser to show correct file type - check out the screenshot

    can someone help me get the file types to show up correctly? It should say the type such as text document, excel spreadsheet...etc. can someone tell me how i should change the fl.GetType.ToString to get it to show up right?

    Here is what it looks like now



    VB Code:
    1. Dim fldr As DirectoryInfo
    2.         Dim fl As FileInfo
    3.         Dim count As Integer
    4.         Dim tempFile
    5.         Dim pos As Integer
    6.  
    7.         Try
    8.             Directory.SetCurrentDirectory(txtPath.Text())
    9.         Catch e As Exception
    10.             MsgBox(e.Message(), MsgBoxStyle.Critical, "")
    11.             Exit Sub
    12.         End Try
    13.  
    14.         listView1.Items.Clear()
    15.  
    16.         Dim currFldr As New DirectoryInfo(Directory.GetCurrentDirectory)
    17.  
    18.         If currFldr.FullName() <> currFldr.Root.ToString Then
    19.             listView1.Items.Add("..", 1)
    20.             count += 1
    21.         End If
    22.  
    23.         For Each fldr In currFldr.GetDirectories()
    24.             If File.Exists(fldr.Name + "\folder.jpg") Then
    25.                 With listView1
    26.                     .Items.Add(fldr.Name, 3)
    27.                     .Items(count).SubItems.Add("")
    28.                     .Items(count).SubItems.Add(fldr.GetType.Name())
    29.                     .Items(count).SubItems.Add(fldr.LastWriteTime)
    30.                 End With
    31.             Else
    32.                 With listView1
    33.                     .Items.Add(fldr.Name, 2)
    34.                     .Items(count).SubItems.Add("")
    35.                     .Items(count).SubItems.Add(fldr.GetType.Name())
    36.                     .Items(count).SubItems.Add(fldr.LastWriteTime)
    37.                 End With
    38.             End If
    39.             count += 1
    40.         Next
    41.  
    42.         For Each fl In currFldr.GetFiles()
    43.             tempFile = fl.Name()
    44.             pos = tempFile.LastIndexOf(".")
    45.  
    46.             If pos > 0 Then
    47.                 tempFile = tempFile.Substring(0, pos)
    48.             End If
    49.  
    50.             If File.Exists(tempFile + ".tbn") Then
    51.                 With listView1
    52.                     .Items.Add(fl.Name, 5)
    53.                     .Items(count).SubItems.Add(Format(Math.Ceiling(fl.Length / 1024), "###,###,###,###,###") & " KB")
    54.                     .Items(count).SubItems.Add(fl.GetType.ToString)
    55.                     .Items(count).SubItems.Add(fl.LastWriteTime)
    56.                 End With
    57.             Else
    58.                 With listView1
    59.                     .Items.Add(fl.Name, 4)
    60.                     .Items(count).SubItems.Add(Format(Math.Ceiling(fl.Length / 1024), "###,###,###,###,###") & " KB")
    61.                     .Items(count).SubItems.Add(fl.GetType.ToString)
    62.                     .Items(count).SubItems.Add(fl.LastWriteTime)
    63.                 End With
    64.             End If
    65.             count += 1
    66.         Next
    Last edited by charvelzeppelin; Jan 16th, 2005 at 02:51 PM.

  2. #2
    Addicted Member
    Join Date
    Jan 2005
    Location
    Montréal
    Posts
    160

    Re: cant get my file browser to show correct file type - check out the screenshot

    Check this code out, notice that I extract the file type from the registry, not the type of the objects in the program themselves :

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.  
    3.         Dim myFileInfo As IO.FileInfo
    4.         Dim myDirInfo As IO.DirectoryInfo
    5.         Dim myLVItem As ListViewItem
    6.  
    7.         For Each sDirPath As String In IO.Directory.GetDirectories("c:\")
    8.  
    9.             myDirInfo = New IO.DirectoryInfo(sDirPath)
    10.  
    11.             myLVItem = New ListViewItem(myDirInfo.Name)
    12.  
    13.             myLVItem.SubItems.Add("")
    14.             myLVItem.SubItems.Add("Folder")
    15.             myLVItem.SubItems.Add(myDirInfo.LastWriteTime)
    16.  
    17.             ListView1.Items.Add(myLVItem)
    18.  
    19.         Next
    20.  
    21.         For Each sFilePath As String In IO.Directory.GetFiles("c:\")
    22.  
    23.             myFileInfo = New IO.FileInfo(sFilePath)
    24.  
    25.             myLVItem = New ListViewItem(myFileInfo.Name)
    26.  
    27.             myLVItem.SubItems.Add(Format(Math.Ceiling(myFileInfo.Length / 1024), "###,###,###,###,###") & " KB")
    28.             myLVItem.SubItems.Add(GetFileTypeFromRegistry(myFileInfo.Extension))
    29.             myLVItem.SubItems.Add(myFileInfo.LastWriteTime)
    30.  
    31.             ListView1.Items.Add(myLVItem)
    32.  
    33.         Next
    34.  
    35.     End Sub
    36.  
    37.     Private Function GetFileTypeFromRegistry(ByVal sFileExtension As String) As String
    38.  
    39.         Dim myRegClsRoot As RegistryKey = Registry.ClassesRoot
    40.         Dim myRegKeyExt As RegistryKey
    41.         Dim myRegKeyFileType As RegistryKey
    42.         Dim sFileType As String
    43.  
    44.         If Not sFileExtension.StartsWith(".") Then
    45.             sFileExtension = "." + sFileExtension
    46.         End If
    47.  
    48.         myRegKeyExt = myRegClsRoot.OpenSubKey(sFileExtension, False)
    49.  
    50.         If myRegKeyExt Is Nothing Then
    51.             Return "Unkown Extension"
    52.         End If
    53.  
    54.         If myRegKeyExt.GetValue("") = "" Then
    55.             myRegKeyFileType.Close()
    56.             Return "Unknown Extension"
    57.         End If
    58.  
    59.         myRegKeyFileType = myRegClsRoot.OpenSubKey(myRegKeyExt.GetValue(""), False)
    60.  
    61.         sFileType = myRegKeyFileType.GetValue("")
    62.  
    63.         myRegKeyFileType.Close()
    64.         myRegKeyExt.Close()
    65.  
    66.         Return sFileType
    67.  
    68.     End Function
    There are no stupid questions, but a whole bunch of dumb sayings !

    Save time on database code, try DataLG !

  3. #3

    Thread Starter
    Member
    Join Date
    Dec 2004
    Posts
    49

    Re: cant get my file browser to show correct file type (resolved)

    thanks, you're the man ....the function works great.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width