但是System.Environment.Version.ToString()缺是2.0的,这个明显不对
什么方法能得到3.5SP1这个值
前提:机器上至少装了2.0版本的.NET,所以不存在.NET存不存在的问题
8 个解决方案
#1
沉的太快 自己顶下
#2
这是老外的 C 版本
http://public.blu.livefilestore.com/y1pnfXzUD5AzWOY_3D8VdqmYHZTVhiC8ratf7oamm40GOCxWm-JqixjBLWLH4rNCUgCoiKmCOIUQMJkhIotHBe6Vw/detectFX.cpp.txt?psid=1
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
// In case the machine this is compiled on does not have the most recent platform SDK
// with these values defined, define them here
#ifndef SM_TABLETPC
#define SM_TABLETPC 86
#endif
#ifndef SM_MEDIACENTER
#define SM_MEDIACENTER 87
#endif
// Constants that represent registry key names and value names
// to use for detection
const TCHAR *g_szNetfx10RegKeyName = _T("Software\\Microsoft\\.NETFramework\\Policy\\v1.0");
const TCHAR *g_szNetfx10RegKeyValue = _T("3705");
const TCHAR *g_szNetfx10SPxMSIRegKeyName = _T("Software\\Microsoft\\Active Setup\\Installed Components\\{78705f0d-e8db-4b2d-8193-982bdda15ecd}");
const TCHAR *g_szNetfx10SPxOCMRegKeyName = _T("Software\\Microsoft\\Active Setup\\Installed Components\\{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}");
const TCHAR *g_szNetfx11RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v1.1.4322");
const TCHAR *g_szNetfx20RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v2.0.50727");
const TCHAR *g_szNetfx30RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0\\Setup");
const TCHAR *g_szNetfx30SpRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0");
const TCHAR *g_szNetfx30RegValueName = _T("InstallSuccess");
const TCHAR *g_szNetfx35RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.5");
const TCHAR *g_szNetfx40ClientRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Client");
const TCHAR *g_szNetfx40FullRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full");
const TCHAR *g_szNetfx40SPxRegValueName = _T("Servicing");
const TCHAR *g_szNetfxStandardRegValueName = _T("Install");
const TCHAR *g_szNetfxStandardSPxRegValueName = _T("SP");
const TCHAR *g_szNetfxStandardVersionRegValueName = _T("Version");
// Version information for final release of .NET Framework 3.0
const int g_iNetfx30VersionMajor = 3;
const int g_iNetfx30VersionMinor = 0;
const int g_iNetfx30VersionBuild = 4506;
const int g_iNetfx30VersionRevision = 26;
// Version information for final release of .NET Framework 3.5
const int g_iNetfx35VersionMajor = 3;
const int g_iNetfx35VersionMinor = 5;
const int g_iNetfx35VersionBuild = 21022;
const int g_iNetfx35VersionRevision = 8;
// Version information for final release of .NET Framework 4
const int g_iNetfx40VersionMajor = 4;
const int g_iNetfx40VersionMinor = 0;
const int g_iNetfx40VersionBuild = 30319;
const int g_iNetfx40VersionRevision = 0;
// Function prototypes
bool CheckNetfxBuildNumber(const TCHAR*, const TCHAR*, const int, const int, const int, const int);
http://public.blu.livefilestore.com/y1pnfXzUD5AzWOY_3D8VdqmYHZTVhiC8ratf7oamm40GOCxWm-JqixjBLWLH4rNCUgCoiKmCOIUQMJkhIotHBe6Vw/detectFX.cpp.txt?psid=1
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
// In case the machine this is compiled on does not have the most recent platform SDK
// with these values defined, define them here
#ifndef SM_TABLETPC
#define SM_TABLETPC 86
#endif
#ifndef SM_MEDIACENTER
#define SM_MEDIACENTER 87
#endif
// Constants that represent registry key names and value names
// to use for detection
const TCHAR *g_szNetfx10RegKeyName = _T("Software\\Microsoft\\.NETFramework\\Policy\\v1.0");
const TCHAR *g_szNetfx10RegKeyValue = _T("3705");
const TCHAR *g_szNetfx10SPxMSIRegKeyName = _T("Software\\Microsoft\\Active Setup\\Installed Components\\{78705f0d-e8db-4b2d-8193-982bdda15ecd}");
const TCHAR *g_szNetfx10SPxOCMRegKeyName = _T("Software\\Microsoft\\Active Setup\\Installed Components\\{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}");
const TCHAR *g_szNetfx11RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v1.1.4322");
const TCHAR *g_szNetfx20RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v2.0.50727");
const TCHAR *g_szNetfx30RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0\\Setup");
const TCHAR *g_szNetfx30SpRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0");
const TCHAR *g_szNetfx30RegValueName = _T("InstallSuccess");
const TCHAR *g_szNetfx35RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.5");
const TCHAR *g_szNetfx40ClientRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Client");
const TCHAR *g_szNetfx40FullRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full");
const TCHAR *g_szNetfx40SPxRegValueName = _T("Servicing");
const TCHAR *g_szNetfxStandardRegValueName = _T("Install");
const TCHAR *g_szNetfxStandardSPxRegValueName = _T("SP");
const TCHAR *g_szNetfxStandardVersionRegValueName = _T("Version");
// Version information for final release of .NET Framework 3.0
const int g_iNetfx30VersionMajor = 3;
const int g_iNetfx30VersionMinor = 0;
const int g_iNetfx30VersionBuild = 4506;
const int g_iNetfx30VersionRevision = 26;
// Version information for final release of .NET Framework 3.5
const int g_iNetfx35VersionMajor = 3;
const int g_iNetfx35VersionMinor = 5;
const int g_iNetfx35VersionBuild = 21022;
const int g_iNetfx35VersionRevision = 8;
// Version information for final release of .NET Framework 4
const int g_iNetfx40VersionMajor = 4;
const int g_iNetfx40VersionMinor = 0;
const int g_iNetfx40VersionBuild = 30319;
const int g_iNetfx40VersionRevision = 0;
// Function prototypes
bool CheckNetfxBuildNumber(const TCHAR*, const TCHAR*, const int, const int, const int, const int);
#3
查询注册表是否安装3.0框架
Request.Browser.ClrVersion.Major
private static bool IsDotNet3VersionInstalled(int major, int minor, int build)
{
bool result = false;
const string regValueName = "InstallSuccess";
if (!result)
{
const string regKeyNameFormat = "Software\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}\\Setup";
string regKeyName = string.Format(regKeyNameFormat, major, minor);
result |= CheckForRegValueEquals1(regKeyName, regValueName);
}
if (!result)
{
const string regKeyNameFormat2 = "Software\\Wow6432Node\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}\\Setup";
string regKeyName2 = string.Format(regKeyNameFormat2, major, minor);
result |= CheckForRegValueEquals1(regKeyName2, regValueName);
}
return result;
}
private static bool CheckForRegValueEquals1(string regKeyName, string regValueName)
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(regKeyName, false))
{
object value = null;
if (key != null)
{
value = key.GetValue(regValueName);
}
return (value != null && value is int && (int)value == 1);
}
}
Request.Browser.ClrVersion.Major
private static bool IsDotNet3VersionInstalled(int major, int minor, int build)
{
bool result = false;
const string regValueName = "InstallSuccess";
if (!result)
{
const string regKeyNameFormat = "Software\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}\\Setup";
string regKeyName = string.Format(regKeyNameFormat, major, minor);
result |= CheckForRegValueEquals1(regKeyName, regValueName);
}
if (!result)
{
const string regKeyNameFormat2 = "Software\\Wow6432Node\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}\\Setup";
string regKeyName2 = string.Format(regKeyNameFormat2, major, minor);
result |= CheckForRegValueEquals1(regKeyName2, regValueName);
}
return result;
}
private static bool CheckForRegValueEquals1(string regKeyName, string regValueName)
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(regKeyName, false))
{
object value = null;
if (key != null)
{
value = key.GetValue(regValueName);
}
return (value != null && value is int && (int)value == 1);
}
}
#4
用GetFileVersion方法获取,这是mscoree.dll中的方法,是个动态链接库,非托管方法。关于mscoree.dll比较复杂,它可以说是CLR的老板,mscoree负责指定本地哪个版本的CLR来运行当前入口Assembly,也就是exe。你可以在msdn中查GetFileVersion,调用它要通过Pinvoke的。你的程序如果用了GetFileVersion,那么发布的时候就必须把mscoree.dll也拷贝进去了,虽然用户机器上只要装了.net framework肯定在System32下有。
#5
#6
我也在想这个应该怎么整
#7
研究半天没找到最稳妥的解决办法 明天继续 睡觉去
#8
读取注册表
#1
沉的太快 自己顶下
#2
这是老外的 C 版本
http://public.blu.livefilestore.com/y1pnfXzUD5AzWOY_3D8VdqmYHZTVhiC8ratf7oamm40GOCxWm-JqixjBLWLH4rNCUgCoiKmCOIUQMJkhIotHBe6Vw/detectFX.cpp.txt?psid=1
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
// In case the machine this is compiled on does not have the most recent platform SDK
// with these values defined, define them here
#ifndef SM_TABLETPC
#define SM_TABLETPC 86
#endif
#ifndef SM_MEDIACENTER
#define SM_MEDIACENTER 87
#endif
// Constants that represent registry key names and value names
// to use for detection
const TCHAR *g_szNetfx10RegKeyName = _T("Software\\Microsoft\\.NETFramework\\Policy\\v1.0");
const TCHAR *g_szNetfx10RegKeyValue = _T("3705");
const TCHAR *g_szNetfx10SPxMSIRegKeyName = _T("Software\\Microsoft\\Active Setup\\Installed Components\\{78705f0d-e8db-4b2d-8193-982bdda15ecd}");
const TCHAR *g_szNetfx10SPxOCMRegKeyName = _T("Software\\Microsoft\\Active Setup\\Installed Components\\{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}");
const TCHAR *g_szNetfx11RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v1.1.4322");
const TCHAR *g_szNetfx20RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v2.0.50727");
const TCHAR *g_szNetfx30RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0\\Setup");
const TCHAR *g_szNetfx30SpRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0");
const TCHAR *g_szNetfx30RegValueName = _T("InstallSuccess");
const TCHAR *g_szNetfx35RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.5");
const TCHAR *g_szNetfx40ClientRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Client");
const TCHAR *g_szNetfx40FullRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full");
const TCHAR *g_szNetfx40SPxRegValueName = _T("Servicing");
const TCHAR *g_szNetfxStandardRegValueName = _T("Install");
const TCHAR *g_szNetfxStandardSPxRegValueName = _T("SP");
const TCHAR *g_szNetfxStandardVersionRegValueName = _T("Version");
// Version information for final release of .NET Framework 3.0
const int g_iNetfx30VersionMajor = 3;
const int g_iNetfx30VersionMinor = 0;
const int g_iNetfx30VersionBuild = 4506;
const int g_iNetfx30VersionRevision = 26;
// Version information for final release of .NET Framework 3.5
const int g_iNetfx35VersionMajor = 3;
const int g_iNetfx35VersionMinor = 5;
const int g_iNetfx35VersionBuild = 21022;
const int g_iNetfx35VersionRevision = 8;
// Version information for final release of .NET Framework 4
const int g_iNetfx40VersionMajor = 4;
const int g_iNetfx40VersionMinor = 0;
const int g_iNetfx40VersionBuild = 30319;
const int g_iNetfx40VersionRevision = 0;
// Function prototypes
bool CheckNetfxBuildNumber(const TCHAR*, const TCHAR*, const int, const int, const int, const int);
http://public.blu.livefilestore.com/y1pnfXzUD5AzWOY_3D8VdqmYHZTVhiC8ratf7oamm40GOCxWm-JqixjBLWLH4rNCUgCoiKmCOIUQMJkhIotHBe6Vw/detectFX.cpp.txt?psid=1
#include <stdio.h>
#include <windows.h>
#include <tchar.h>
#include <strsafe.h>
// In case the machine this is compiled on does not have the most recent platform SDK
// with these values defined, define them here
#ifndef SM_TABLETPC
#define SM_TABLETPC 86
#endif
#ifndef SM_MEDIACENTER
#define SM_MEDIACENTER 87
#endif
// Constants that represent registry key names and value names
// to use for detection
const TCHAR *g_szNetfx10RegKeyName = _T("Software\\Microsoft\\.NETFramework\\Policy\\v1.0");
const TCHAR *g_szNetfx10RegKeyValue = _T("3705");
const TCHAR *g_szNetfx10SPxMSIRegKeyName = _T("Software\\Microsoft\\Active Setup\\Installed Components\\{78705f0d-e8db-4b2d-8193-982bdda15ecd}");
const TCHAR *g_szNetfx10SPxOCMRegKeyName = _T("Software\\Microsoft\\Active Setup\\Installed Components\\{FDC11A6F-17D1-48f9-9EA3-9051954BAA24}");
const TCHAR *g_szNetfx11RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v1.1.4322");
const TCHAR *g_szNetfx20RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v2.0.50727");
const TCHAR *g_szNetfx30RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0\\Setup");
const TCHAR *g_szNetfx30SpRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.0");
const TCHAR *g_szNetfx30RegValueName = _T("InstallSuccess");
const TCHAR *g_szNetfx35RegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v3.5");
const TCHAR *g_szNetfx40ClientRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Client");
const TCHAR *g_szNetfx40FullRegKeyName = _T("Software\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full");
const TCHAR *g_szNetfx40SPxRegValueName = _T("Servicing");
const TCHAR *g_szNetfxStandardRegValueName = _T("Install");
const TCHAR *g_szNetfxStandardSPxRegValueName = _T("SP");
const TCHAR *g_szNetfxStandardVersionRegValueName = _T("Version");
// Version information for final release of .NET Framework 3.0
const int g_iNetfx30VersionMajor = 3;
const int g_iNetfx30VersionMinor = 0;
const int g_iNetfx30VersionBuild = 4506;
const int g_iNetfx30VersionRevision = 26;
// Version information for final release of .NET Framework 3.5
const int g_iNetfx35VersionMajor = 3;
const int g_iNetfx35VersionMinor = 5;
const int g_iNetfx35VersionBuild = 21022;
const int g_iNetfx35VersionRevision = 8;
// Version information for final release of .NET Framework 4
const int g_iNetfx40VersionMajor = 4;
const int g_iNetfx40VersionMinor = 0;
const int g_iNetfx40VersionBuild = 30319;
const int g_iNetfx40VersionRevision = 0;
// Function prototypes
bool CheckNetfxBuildNumber(const TCHAR*, const TCHAR*, const int, const int, const int, const int);
#3
查询注册表是否安装3.0框架
Request.Browser.ClrVersion.Major
private static bool IsDotNet3VersionInstalled(int major, int minor, int build)
{
bool result = false;
const string regValueName = "InstallSuccess";
if (!result)
{
const string regKeyNameFormat = "Software\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}\\Setup";
string regKeyName = string.Format(regKeyNameFormat, major, minor);
result |= CheckForRegValueEquals1(regKeyName, regValueName);
}
if (!result)
{
const string regKeyNameFormat2 = "Software\\Wow6432Node\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}\\Setup";
string regKeyName2 = string.Format(regKeyNameFormat2, major, minor);
result |= CheckForRegValueEquals1(regKeyName2, regValueName);
}
return result;
}
private static bool CheckForRegValueEquals1(string regKeyName, string regValueName)
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(regKeyName, false))
{
object value = null;
if (key != null)
{
value = key.GetValue(regValueName);
}
return (value != null && value is int && (int)value == 1);
}
}
Request.Browser.ClrVersion.Major
private static bool IsDotNet3VersionInstalled(int major, int minor, int build)
{
bool result = false;
const string regValueName = "InstallSuccess";
if (!result)
{
const string regKeyNameFormat = "Software\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}\\Setup";
string regKeyName = string.Format(regKeyNameFormat, major, minor);
result |= CheckForRegValueEquals1(regKeyName, regValueName);
}
if (!result)
{
const string regKeyNameFormat2 = "Software\\Wow6432Node\\Microsoft\\NET Framework Setup\\NDP\\v{0}.{1}\\Setup";
string regKeyName2 = string.Format(regKeyNameFormat2, major, minor);
result |= CheckForRegValueEquals1(regKeyName2, regValueName);
}
return result;
}
private static bool CheckForRegValueEquals1(string regKeyName, string regValueName)
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(regKeyName, false))
{
object value = null;
if (key != null)
{
value = key.GetValue(regValueName);
}
return (value != null && value is int && (int)value == 1);
}
}
#4
用GetFileVersion方法获取,这是mscoree.dll中的方法,是个动态链接库,非托管方法。关于mscoree.dll比较复杂,它可以说是CLR的老板,mscoree负责指定本地哪个版本的CLR来运行当前入口Assembly,也就是exe。你可以在msdn中查GetFileVersion,调用它要通过Pinvoke的。你的程序如果用了GetFileVersion,那么发布的时候就必须把mscoree.dll也拷贝进去了,虽然用户机器上只要装了.net framework肯定在System32下有。
#5
#6
我也在想这个应该怎么整
#7
研究半天没找到最稳妥的解决办法 明天继续 睡觉去
#8
读取注册表