C# 檔案對應fiel Content Tyoe


private static string GetContentTypeForFileName(string fileName) 
    string ext = System.IO.Path.GetExtension(fileName); 
    using (Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext)) 
    { 
        if (registryKey == null) 
            return null; 
        var value = registryKey.GetValue("Content Type"); 
        return (value == null) ? string.Empty : value.ToString(); 
    } 
完整範例如下:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
namespace ConsoleApplication10 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            const string file = @"C:\Users\Public\Music\Sample Music\Sleep Away.mp3"; 
            Console.WriteLine(GetContentTypeForFileName(file)); 
        } 
        private static string GetContentTypeForFileName(string fileName) 
        { 
            string ext = System.IO.Path.GetExtension(fileName); 
            using (Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext)) 
            { 
                if (registryKey == null) 
                    return null; 
                var value = registryKey.GetValue("Content Type"); 
                return (value == null) ? string.Empty : value.ToString(); 
            } 
        }    } 
}

留言

這個網誌中的熱門文章

C# 在 GDI+ 中發生泛型錯誤

[LeetCode] Robot Return to Origin