473,408 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

sample code for finding type of file based on extension

Just thought maybe someone here would like to know this. It's an example
code I just created quickly on how to figure out the name of a type of file
based on its extension (say for example .DOC) would return "Microsoft Word
Document"... tired doing this before with SHGetFileInfo or what ever its
called, but that seemed to require a phyiscal file to be there, where this
you can just give it a file extension with no file.

Private Function GetNameForExtension(ByVal Extension As String) As String

' open the class root tree in registry and get the node based on the
extension

Dim regProgramKeys As RegistryKey =
Registry.ClassesRoot.OpenSubKey(Extension.Trim)

Dim s_tempName As String = String.Empty

' find the default value of the extensions node, if there is none then use
the extension

Dim s_ClassName As String = regProgramKeys.GetValue("",
Extension.Trim).ToString

' if we got a class name back from the registry for that extension, look up
its name

' else return the extension as the name

If s_ClassName.Trim = Extension.Trim Then

s_tempName = Extension.Trim

Else

' try to look up the class name's formal name, if it doesn't have one return
the extension only

Dim regItemKeys As RegistryKey =
Registry.ClassesRoot.OpenSubKey(s_ClassName)

s_tempName = regItemKeys.GetValue("", Extension.Trim).ToString

End If

Return s_tempName

End Function


Nov 21 '05 #1
1 2151
slight update to that code, since it didn't handle unlisted extensions in
the registry

' returns the name of the type of file based on extension

Public Shared Function GetNameForExtension(ByVal Extension As String) As
String

' open the class root tree in registry and get the node based on the
extension

Dim regProgramKeys As RegistryKey =
Registry.ClassesRoot.OpenSubKey(Extension.Trim)

Dim s_tempName As String = String.Empty

' find the default value of the extensions node, if there is none then use
the extension

If Not regProgramKeys Is Nothing Then

Dim s_ClassName As String = regProgramKeys.GetValue("",
Extension.Trim).ToString

' if we got a class name back from the registry for that extension, look up
its name

' else return the extension as the name

If (s_ClassName.Trim = Extension.Trim) Then

s_tempName = Extension.Trim

Else

' try to look up the class name's formal name, if it doesn't have one return
the extension only

Dim regItemKeys As RegistryKey =
Registry.ClassesRoot.OpenSubKey(s_ClassName)

If Not regItemKeys Is Nothing Then

s_tempName = regItemKeys.GetValue("", Extension.Trim).ToString

Else

s_tempName = Extension.Trim

End If

End If

Else

s_tempName = Extension.Trim

End If

Return s_tempName

End Function
Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
5
by: hokiegal99 | last post by:
Hi, I have a working Python script that renames files that don't currently have PC based file extensions. For example, if there is a MS Word file that does not have '.doc' on the end of it, the...
13
by: Darren Dale | last post by:
Some time ago I asked about executing a python program or script. For windows, I was informed that the .py extension could be added to some list of executable extensions, and then I could just type...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
3
by: Jonny | last post by:
Hi all, I need to find out the hex value of a file. Is this possible? I need to be able to quickly access and confirm its extension against its file type (which is as far as i know defined by its...
4
by: Ganesh Muthuvelu | last post by:
Hello, Let us say that I have a schema or complex type like this one below: Is there a way programtically or a tool to create sample XML file/data for the schema?. I have number of such complex...
3
by: Alexander van Doormalen | last post by:
I have a windows service which calls extensions. In 1 of those extensions I want to load a config file (extension.dll.config). In that config file I defined some ConfigurationSection's. For...
5
by: rAinDeEr | last post by:
Hi, Can any one show me a sample create table structure for storing .BMP's and .GIF images. togther with its description. Thanks in advance, rAinDeEr
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.