这一节主要讲检测,下一节讲安装!
一个c#制作完的工具一运行,一个可以不用检测与安装的就是framework环境了,要是没这东西,也运行不起来。
这里提供几个检测的范围:
一.检测操作系统版本
二.检测iis版本
三.检测framework版本
四.检测rar工具是否安装
以下为具体实现:
一.检测操作系统版本:
我们通过:system.environment.osversion.version 可以获取到操作系统的版本。
之后,我们要根据版本号,进行if else if或switch之类的分支判断即可:
public static string getosystemname()
{
return getosystemnamebyversion(system.environment.osversion.version) + "\r\n" + system.environment.osversion.servicepack;
}
private static string getosystemnamebyversion(version version)
if (version.major == 5 && version.minor == 2)
{
return "microsoft windows server 2003";
}
else if (version.major == 5 && version.minor == 1)
return "microsoft windows xp ";
else if (version.major == 5 && version.minor == 0)
return "microsoft windows 2000 ";
else if (version.major <= 4)
return "microsoft windows nt ";
return "未知";
这里主要是通过注册表进行判断:
我们通过获取:hkey_local_machine\software\microsoft\inetstp下的
主版本号majorversion 和
次版本号minorversion 来判断iis的版本
public static string getiisverstion()
registrykey key = registry.localmachine.opensubkey(@"software\microsoft\inetstp");
if (key == null) { return ""; }
return convert.tostring(key.getvalue("majorversion")) + "." + convert.tostring(key.getvalue("minorversion"));
和检测iis一样,可以通过注册表检测:
注册表路径:
1.1版本:hkey_local_machine\software\microsoft\net framework setup\ndp\v1.1.4322
2.0版本:hkey_local_machine\software\microsoft\net framework setup\ndp\v2.0.50727
3.0版本:hkey_local_machine\software\microsoft\net framework setup\ndp\v3.0\setup
3.5版本:hkey_local_machine\software\microsoft\net framework setup\ndp\v3.5
只要判断注册表节点是否存在即可!代码参考iis检测。
四.检测rar工具
这里我是通过检测rar.exe文件的常见安装路径来判断检测:
常见路径:c:\program files\winrar\winrar.exe
这里同样检测d盘,e盘路径:
public static bool isfileexistsbycde(string path)
bool exists=true;
if (!system.io.file.exists(path))
path = path.replace("c:", "d:");
if (!system.io.file.exists(path))
{
path = path.replace("d:", "e:");
if (!system.io.file.exists(path))
{
exists = false;
}
}
return exists;