通过注册表获取文件的ContentType


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace ObtainContenttype
{
    
class Program
    {
        
static void Main(string[] args)
        {
            GetFileContentType(
@"C:\Documents and Settings\roboth_staff\桌面\spx.txt");
        }
        
public static string GetFileContentType(string filename)
        {
            
string[] array = filename.Split('.');
            
string result = string.Empty;
            
string suffix = "." + array[array.Length - 1];
            RegistryKey rg 
= Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(suffix);
            
object obj = rg.GetValue("Content Type");
            result 
= obj != null ? obj.ToString() : string.Empty;
            rg.Close();
            
return result;
        }
    }
}
posted @ 2009-07-02 10:05  roboth  阅读(525)  评论(0编辑  收藏  举报