How to code to get direct X version on my machine in C#? [duplicate]

核能气质少年 提交于 2019-11-30 17:51:50

问题


I want to know when the user clicks the menu item he should be able to know the direct X version installed on his machine?

I want to code this in C# in VS2008.

What should i write in the menu item click event? Am a beginner in C#,so dont know where to start from..

can anybody please help? Thanks..


回答1:


This may help: .NET How to detect if DirectX 10 is supported?.

Edit:

Below is some better code I think. The best I can come up with is a check based on Windows version in the case of DX10 or DX11. This is not 100% accurate (because Vista can be upgraded to DX11 but I do not check for this), but better than nothing.

    private int GetDirectxMajorVersion()
    {
        int directxMajorVersion = 0;

        var OSVersion = Environment.OSVersion;

        // if Windows Vista or later
        if (OSVersion.Version.Major >= 6)
        {
            // if Windows 7 or later
            if (OSVersion.Version.Major > 6 || OSVersion.Version.Minor >= 1)
            {
                directxMajorVersion = 11;
            }
            // if Windows Vista
            else
            {
                directxMajorVersion = 10;
            }
        }
        // if Windows XP or earlier.
        else
        {
            using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\DirectX"))
            {
                string versionStr = key.GetValue("Version") as string;
                if (!string.IsNullOrEmpty(versionStr))
                {
                    var versionComponents = versionStr.Split('.');
                    if (versionComponents.Length > 1)
                    {
                        int directXLevel;
                        if (int.TryParse(versionComponents[1], out directXLevel))
                        {
                            directxMajorVersion = directXLevel;
                        }
                    }
                }
            }
        }

        return directxMajorVersion;
    }



回答2:


Found this question linked in another but I see the solution is not peffect so I post my idea of how to solve the problem. But beware: it very, very slow.

EDIT: Removed registry check method because it works only for Dx <=9 (thx @Telanor)

This method is very, very slow, but only one I figured out that is 100% accurate

private static int checkdxversion_dxdiag()
{
    Process.Start("dxdiag", "/x dxv.xml");
    while (!File.Exists("dxv.xml"))
        Thread.Sleep(1000);
    XmlDocument doc = new XmlDocument();
    doc.Load("dxv.xml");
    XmlNode dxd = doc.SelectSingleNode("//DxDiag");
    XmlNode dxv = dxd.SelectSingleNode("//DirectXVersion");

    return Convert.ToInt32(dxv.InnerText.Split(' ')[1]);
}


来源:https://stackoverflow.com/questions/6159850/how-to-code-to-get-direct-x-version-on-my-machine-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!