473,800 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 GetNameForExten sion(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.Classe sRoot.OpenSubKe y(Extension.Tri m)

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.Tri m = 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.Classe sRoot.OpenSubKe y(s_ClassName)

s_tempName = regItemKeys.Get Value("", Extension.Trim) .ToString

End If

Return s_tempName

End Function


Nov 21 '05 #1
1 2177
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 GetNameForExten sion(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.Classe sRoot.OpenSubKe y(Extension.Tri m)

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.Tr im = 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.Classe sRoot.OpenSubKe y(s_ClassName)

If Not regItemKeys Is Nothing Then

s_tempName = regItemKeys.Get Value("", 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
2418
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 narrowed it down to the Scrubber.py file.. If this seems like a quick step me through, I would be very appreciative, could get you something on your Amazon wish-list (that is me on my knees begging).. From just my basic understanding, it...
5
2036
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 script will append that. The script also knows to *not* add an extension if the file already has one. Bascially, here's how I'm doing it: for fname in files: doc_id = string.find(file(os.path.join(root,fname),
13
2287
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 "mycode" instead of "python mycode.py". Great advice, worked like a charm. I recently jumped ship, and have been running Gentoo Linux for about two months. Is it possible to get the same behavior on Linux? I thought it would have something to...
2
3793
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 produce two messages having the same message digest. That conjecture is false, as demonstrated by Wang, Feng, Lai and Yu in 2004 . Just recently, Wang, Yu, and Lin showed a short- cut solution for finding collisions in SHA-1 . Their result
1
4011
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 live databases on the network. Is there a way, via code, when I get back in-house from being on the road to click a button, and select the backends I want to link to? I would want to delete all the current links and link to the "live"
3
2131
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 hex value). Thanks, /Jonny
4
4543
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 tpyer/schemas for which I need to create sample XMLs so I thought of doing it programatically.. Any pointers would be appreciated. Thanks.
3
1970
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 example: <section name="somename" type="sonenamespace.someclassname, someassembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/> The loading part is done using ConfigurationManager.OpenMappedExeConfiguration(ExeConfigurationFileMap,...
5
4420
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
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7580
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6813
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.