在使用 .Net Framwork 時讀取 MediaPath 目錄中的內容(續)

(續前篇)Windows 系統中,關於系統音效檔案位置的相關資訊,是寫在 Registry 中,不過,如果是照著前篇實作,應該(會像我一樣)很快就會發現一個問題:在 Windows Vista 裡頭,這段資訊的寫法,與 Windows XP 不太一樣:Windows XP 寫在 MediaPath 這個字串值裡頭,Windows Vista 則是寫在 MediaPathUnexpanded 裡頭。

所以,要抓取 Windows Vista 的系統音效目錄,要用下面這段 code:

RegistryKey registryKey = Registry.LocalMachine;
registryKey = registryKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion", false);
string MediaPathUnexpanded = registryKey.GetValue("MediaPathUnexpanded").ToString();
m_mediaPath = Environment.ExpandEnvironmentVariables(MediaPathUnexpanded);

另外,就是要判斷現在到底是在 Windows Vista 上,還是 XP 上:

public static bool IsVistaOrLater
{
    get
    {
        return Environment.OSVersion.Platform ==
            PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 6;
    }
}

public static bool IsXp
{
    get
    {
        return Environment.OSVersion.Platform ==
            PlatformID.Win32NT && Environment.OSVersion.Version.Major >= 5;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.