Get File Type

Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 75
Reputation: mansi sharma is an unknown quantity at this point 
Solved Threads: 0
mansi sharma mansi sharma is offline Offline
Junior Poster in Training

Get File Type

 
0
  #1
Aug 15th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 2
Reputation: engineerik is an unknown quantity at this point 
Solved Threads: 1
engineerik engineerik is offline Offline
Newbie Poster

Re: Get File Type

 
0
  #2
Aug 16th, 2009
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.

  1.  
  2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3. Dim readValue As String
  4.  
  5. readValue = My.Computer.Registry.GetValue _
  6. ("HKEY_CLASSES_ROOT\" & TextBox1.Text, "", "ExtensionNotFound")
  7. MsgBox("The value is " & readValue)
  8.  
  9. End Sub
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,609
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 460
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Get File Type

 
0
  #3
Aug 16th, 2009
Read this article - http://www.cryer.co.uk/filetypes/
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 12
Reputation: JadeSimon is an unknown quantity at this point 
Solved Threads: 4
JadeSimon JadeSimon is offline Offline
Newbie Poster

Re: Get File Type

 
0
  #4
Aug 16th, 2009
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.

  1. Imports Microsoft.Win32
  2.  
  3. Public Shared Function GetFileType(ByVal fileExtension As String) As String
  4.  
  5. Dim fileType As String = ""
  6.  
  7. 'Search all keys under HKEY_CLASSES_ROOT
  8. For Each subKey As String In Registry.ClassesRoot.GetSubKeyNames()
  9. If String.IsNullOrEmpty(subKey) Then
  10. Continue For
  11. End If
  12.  
  13. If subKey.CompareTo(fileExtension) = 0 Then
  14. 'File extension found. Get Default Value
  15. Dim defaultValue As String = Registry.ClassesRoot.OpenSubKey(subKey).GetValue("").ToString()
  16.  
  17. If defaultValue.Length = 0 Then
  18. 'No File Type specified
  19. Exit For
  20. End If
  21.  
  22. If fileType.Length = 0 Then
  23. 'Get Initial File Type and search for the full File Type Description
  24. fileType = defaultValue
  25. fileExtension = fileType
  26. Else
  27. 'File Type Description found
  28. If defaultValue.Length > 0 Then
  29. fileType = defaultValue
  30. End If
  31.  
  32. Exit For
  33. End If
  34. End If
  35.  
  36. Next
  37.  
  38. Return fileType
  39.  
  40. End Function
  41.  
  42. 'Example USAGE
  43. Dim fileType = GetFileType(".txt")
Last edited by JadeSimon; Aug 16th, 2009 at 3:04 am. Reason: Added the imports statement
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: mansi sharma is an unknown quantity at this point 
Solved Threads: 0
mansi sharma mansi sharma is offline Offline
Junior Poster in Training

Re: Get File Type

 
0
  #5
Aug 16th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC