943,699 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 6921
  • VB.NET RSS
Aug 15th, 2009
0

Get File Type

Expand Post »
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.
Similar Threads
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
mansi sharma is offline Offline
75 posts
since Apr 2008
Aug 16th, 2009
0

Re: Get File Type

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.

VB.NET Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
engineerik is offline Offline
4 posts
since Aug 2009
Aug 16th, 2009
0

Re: Get File Type

Read this article - http://www.cryer.co.uk/filetypes/
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 16th, 2009
0

Re: Get File Type

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.

vb.net Syntax (Toggle Plain Text)
  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
Reputation Points: 20
Solved Threads: 5
Newbie Poster
JadeSimon is offline Offline
12 posts
since Aug 2009
Aug 16th, 2009
1

Re: Get File Type

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.
Reputation Points: 17
Solved Threads: 0
Junior Poster in Training
mansi sharma is offline Offline
75 posts
since Apr 2008
Feb 15th, 2010
0
Re: Get File Type
Thanks for the aritcle
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ravipchandra is offline Offline
1 posts
since Feb 2010
Feb 15th, 2010
0
Re: Get File Type
ravipchandra,

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.
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
This thread is currently closed and is not accepting any new replies.
Previous Thread in VB.NET Forum Timeline: How to i priview image and then how to rotate that image
Next Thread in VB.NET Forum Timeline: Pllleassee help with vb.net and excel!!!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC