can somebody tell me how to get file type..of any file..For .txt file file type is Text Document,for doc file - Microsoft Word Document & so on.

Plz help me out,I m not finding anything on the net related to it.

7 Years
Discussion Span
Last Post by __avd

You need to read the registry to get that info.
Look in HKEY_CLASSES_ROOT. Each extension registered on that computer will have a key in this section of the registry. For instance there will be a .txt key and a .doc key. Add a textbox and a button to a form and enter ".txt" in the textbox and click the button.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim readValue As String

        readValue = My.Computer.Registry.GetValue _
        ("HKEY_CLASSES_ROOT\" & TextBox1.Text, "", "ExtensionNotFound")
        MsgBox("The value is " & readValue)

    End Sub

The following code was tested on Windows 7 RC. It should work on all the older operating systems too, but let me know if you face any problems.

Imports Microsoft.Win32

Public Shared Function GetFileType(ByVal fileExtension As String) As String

        Dim fileType As String = ""

        'Search all keys under HKEY_CLASSES_ROOT
        For Each subKey As String In Registry.ClassesRoot.GetSubKeyNames()
            If String.IsNullOrEmpty(subKey) Then
                Continue For
            End If

            If subKey.CompareTo(fileExtension) = 0 Then
                'File extension found. Get Default Value
                Dim defaultValue As String = Registry.ClassesRoot.OpenSubKey(subKey).GetValue("").ToString()

                If defaultValue.Length = 0 Then
                    'No File Type specified
                    Exit For
                End If

                If fileType.Length = 0 Then
                    'Get Initial File Type and search for the full File Type Description
                    fileType = defaultValue
                    fileExtension = fileType
                    'File Type Description found
                    If defaultValue.Length > 0 Then
                        fileType = defaultValue
                    End If

                    Exit For
                End If
            End If


        Return fileType

End Function

'Example USAGE
Dim fileType = GetFileType(".txt")

First of all thx to u all.
hi engineerik ,ur code is not working...
hi JadeSimon ,Ur code is working 100% correctly on windows Xp.I just have to check whether its running in Windows Vista or not.



You're welcome. I'm glad you found is useful. Please read daniweb member rules. If you want to ask question, start your own thread.

Thread Closed.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.