[.NET]依ProgID到Registry取得檔案的版本號
有人問如何Check Office MODI的版本,不是直接用檔案總管看一下就好了嗎?
但是如果透過程式來告訴我們呢? 那就要寫程式了~~~
我的方式是透過Registry來找到該檔案路徑,再讀取該檔案的屬性,就知道了!
1.先到HKEY_CLASSES_ROOT\依ProgID找到它的CLSID
2.再到HKEY_CLASSES_ROOT\CLSID依剛才找到的CLSID中InprocServer32資料夾的值就是該COM檔案的所在路徑。
所以,只要有ProgID的話,就可以這樣做哦! 程式如下,
private int GetFileVersionByProgID(string progID)
{
		
    int result = -1;
		
    RegistryKey progRegKey = Registry.ClassesRoot.OpenSubKey(progID);
		
    if (progRegKey != null)
		
    {
		
        //1.先到HKEY_CLASSES_ROOT\依ProgID找到它的CLSID
		
        RegistryKey clsidKey = progRegKey.OpenSubKey("CLSID");
		
        string comClsidKey = clsidKey.GetValue("") as string;
		
        if (!string.IsNullOrEmpty(comClsidKey))
		
        {
		
            //2.再到HKEY_CLASSES_ROOT\CLSID依剛才找到的CLSID中InprocServer32資料夾的值就是該COM檔案的所在路徑。
		
            RegistryKey rootCLSID = Registry.ClassesRoot.OpenSubKey("CLSID");
		
            RegistryKey findByCLSID = rootCLSID.OpenSubKey(comClsidKey);
		
            RegistryKey inprocServer32Key = findByCLSID.OpenSubKey("InprocServer32");
		
            string installPath = inprocServer32Key.GetValue("") as string;
		
            System.Diagnostics.FileVersionInfo fileVersInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(installPath);
		
            //check file path
		
            result = fileVersInfo.FileMajorPart;
		
        }
		
    }
		
    return result;
		}
Hi,
亂馬客Blog已移到了 「亂馬客 : Re:從零開始的軟體開發生活」
請大家繼續支持 ^_^

