How to code to get direct X version on my machine in C#? [duplicate]
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..
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;
}
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]);
}
精彩评论