In classic Asp we used the File.Type property to get the friendly name associated with a file type from the registry (e.g. "Text Document" for ".txt"). The FileInfo class which generally replaces the old COM object doesn't replicate this feature and so far I'm not having much success in my search of a replacement.
Asked
Active
Viewed 375 times
0
-
possible duplicate of http://stackoverflow.com/questions/1437382/get-file-type-in-net – Helen Apr 18 '10 at 08:50
-
I think it has been answered here: http://stackoverflow.com/questions/1437382/get-file-type-in-c – Vladimir Georgiev Oct 17 '09 at 20:45
1 Answers
1
I'm not aware of a method in the BCL, but you could easily read it from the Registry:
using System;
using Microsoft.Win32;
class Program
{
static void Main(string[] args)
{
string extension = ".txt";
string nicename = "";
using (RegistryKey key = Registry.ClassesRoot.OpenSubKey(extension))
{
if (key != null)
{
string filetype = key.GetValue(null) as string;
using (RegistryKey keyType = Registry.ClassesRoot.OpenSubKey(filetype))
{
if (keyType != null)
{
nicename = keyType.GetValue(null) as string;
}
}
}
}
Console.WriteLine(nicename);
}
}
However, the method used in the link provided by Vladimir is to be preferred as it uses an API interface.

Community
- 1
- 1

Dirk Vollmar
- 172,527
- 53
- 255
- 316
-
I was hoping to avoid reading the values manually but given the options I may may go this route – John Lewin Oct 17 '09 at 21:06