<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>Get the file extension from a file path or file name</Title>
      <Shortcut>Getthefileextensionfromafilepathorfilename</Shortcut>
      <Description>Get the file extension from a file path or file name [VB.NET]</Description>
      <Author>Antonio Cosano</Author>
      <HelpUrl>/PreviewSnippet.aspx?SnippetID=ac4ef358-4ea7-42fc-979b-b259e28a0761</HelpUrl>
      <SnippetTypes>
        <SnippetType>SurroundsWith</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="VB"><![CDATA[Imports Microsoft.Win32
Imports System.IO
' How to call the function
' Get the file extension from a file path or file name
'  -> Dim Extension As String = Path.GetExtension("hello.mp3")
' Get the file type from the extension
' -> Dim FileType = GetFileType(Extension)
Public Shared Function GetFileType(ByVal fileExtension As String) As String
		Dim fileType As String = String.Empty
		'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
				Else
					'File Type Description found
					If defaultValue.Length > 0 Then
						fileType = defaultValue
					End If
					Exit For
				End If
			End If
		Next
		Return fileType
	End Function]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>