求助:C#.net如何检测系统的版本?

lanbing8023 2012-03-21 10:16:39
我的想法是让程序在指定的系统版本(Windows7)中运行,具体为:点击一个按钮,程序去判断系统版本,如果是指定的系统则运行程序,否则输出“此软件只能在Windows7中运行”,各位大大具体该怎么做呢?谢谢了!
...全文
1383 45 打赏 收藏 转发到动态 举报
写回复
用AI写文章
45 条回复
切换为时间正序
请发表友善的回复…
发表回复
开发者孙小聪 2012-03-24
  • 打赏
  • 举报
回复
先把整个显示出来 再取某一部分就行吧
lanbing8023 2012-03-23
  • 打赏
  • 举报
回复
具体的代码怎么实现呢?[Quote=引用 32 楼 dinosaur_1985 的回复:]
我查了一下,你就判断他是不是nt6.1就可以了,截取字符串的时候改成-20就可以了。

Windows NT 6.1 = WIN7
Windows NT 6.0 = WINVISTA
Windows NT 5.1 = WINXP
Windows 4.9 = WINME
Windows NT 5.0 = WIN2K
Windows 4.1 = WIN98SE
Windows 4.01……
[/Quote]
Lugyedo 2012-03-23
  • 打赏
  • 举报
回复
Environment.OSVersion
二号匪 2012-03-23
  • 打赏
  • 举报
回复
[Quote=引用 40 楼 guliang21 的回复:]

public static string GetOSType()
{
Version ver = System.Environment.OSVersion.Version;
string OSType = "";
if (ver.Major == 5 && ver.Minor == 0……
[/Quote]
楼上正解

通过version的各个属性可以得到主版本号和副版本号

操作系统版本号可以参靠
http://news.mydrivers.com/1/130/130047.htm

微软Windows版本号一览
2009-03-16 10:26:20 18524 人阅读 作者:萧萧 编辑:萧萧 [复制链接] [我要爆料]
虽然Windows的命名取决于很多因素,形式各不相同,但是其内部版本号却是一脉相承的,从最初的Windows 1.0到之后的Windows 2.0、Windows 3.0,再到Windows 95,其版本号为Windows 4.0,随后的Windows 9x均为Windows 4系列。

在Windows 7发布之前,微软曾简要提及各个Windows的版本号,并以此解释为何要将下一代操作系统命名为Windows 7,其实,Windows 7的版本号和Vista一样依然是Windows 6系列,Windows 7的兼容性如此良好也得益于这点。

微软MSDN技术网站给出了当前使用的各个操作系统的版本号列表,算是一个简单的总结,以便帮助Windows用户了解其正在使用的系统。

操作系统
版本号
Windows 7
6.1
Windows Server 2008 R2
6.1
Windows Server 2008
6.0
Windows Vista
6.0
Windows Server 2003 R2
5.2
Windows Server 2003
5.2
Windows XP
5.1
Windows 2000
5.0
丹枫无迹 2012-03-23
  • 打赏
  • 举报
回复
public static string GetOSType()
{
Version ver = System.Environment.OSVersion.Version;
string OSType = "";
if (ver.Major == 5 && ver.Minor == 0)
{
OSType = "Windows 2000";
}
else if (ver.Major == 5 && ver.Minor == 1)
{
OSType = "Windows XP";
}
else if (ver.Major == 5 && ver.Minor == 2)
{
OSType = "Windows 2003";
}
else if (ver.Major == 6 && ver.Minor == 0)
{
OSType = "Windows Vista";
}
else if (ver.Major == 6 && ver.Minor == 1)
{
OSType = "Windows7";
}
else
{
OSType = "未知";
}
return OSType;
}
DinoSaur_1985 2012-03-23
  • 打赏
  • 举报
回复
给你优化了一下,你复制过去就行了。

System.OperatingSystem OS = Environment.OSVersion;

string systemOsVersion = null;

switch (OS.VersionString.Remove(OS.VersionString.Length - 20).Trim())
{
case "Microsoft Windows NT 6.1":
systemOsVersion = "WIN7";
break;
case "Microsoft Windows NT 6.0":
systemOsVersion = "WINVISTA";
break;
case "Microsoft Windows NT 5.1":
systemOsVersion = "WINXP";
break;
case "Microsoft Windows NT 5.0":
systemOsVersion = "WIN2000";
break;
case "Microsoft Windows 4.9":
systemOsVersion = "WINME";
break;
case "Microsoft Windows 4.1":
systemOsVersion = "WIN98SE";
break;
case "Microsoft Windows 4.01998":
systemOsVersion = "WIN98";
break;
default:
systemOsVersion = null;
break;
}

if (systemOsVersion == "WIN7")
{
DialogResult exitResult = MessageBox.Show("您当前的系统为:" + systemOsVersion + "\n" + "此软件只针对Windows 7系统使用,您确认要继续吗?", "弹出框title", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

if (exitResult == DialogResult.Cancel)
{
this.Close();
}
}
else
{
MessageBox.Show("您的系统为:" + systemOsVersion + "\n" + "可以运行此软件!");
}
DinoSaur_1985 2012-03-23
  • 打赏
  • 举报
回复

//获取系统版本信息
System.OperatingSystem o = Environment.OSVersion;

if (a.VersionString.Remove(o.VersionString.Length - 20).Trim() == "Microsoft Windows NT 6.1.7601")
{
MessageBox.Show("您的系统为:" + o.VersionString + "\n" + "可以运行此软件!");
}
else
{
DialogResult exitResult = MessageBox.Show("您当前的系统为:"+o.VersionString+"\n"+"此软件只针对Windows 7系统使用,您确认要继续吗?", "弹出框title", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

if (exitResult == DialogResult.OK)
{
this.Close();
}
}
lanbing8023 2012-03-23
  • 打赏
  • 举报
回复
我的想法是去判断并给出系统版本Windows NT 6.1 = WIN7
Windows NT 6.0 = WINVISTA
Windows NT 5.1 = WINXP
Windows 4.9 = WINME
Windows NT 5.0 = WIN2K
Windows 4.1 = WIN98SE
Windows 4.01……
然后再去询问[Quote=引用 36 楼 dinosaur_1985 的回复:]
你就把你-14改成-20
[/Quote]
DinoSaur_1985 2012-03-23
  • 打赏
  • 举报
回复
你就把你-14改成-20
lanbing8023 2012-03-22
  • 打赏
  • 举报
回复
什么办法呢?[Quote=引用 29 楼 chenandczh 的回复:]
如果想更智能化,应该要想想办法...
[/Quote]
lanbing8023 2012-03-22
  • 打赏
  • 举报
回复
貌似Win7没有安装SP1前是Windows NT 6.1.7600[Quote=引用 28 楼 dinosaur_1985 的回复:]
那 Windows NT 6.1.7601就应该是WIN7
[/Quote]
绿领巾童鞋 2012-03-22
  • 打赏
  • 举报
回复
如果想更智能化,应该要想想办法...
安静写代码 2012-03-22
  • 打赏
  • 举报
回复
学习下,备用
liunanxuan613 2012-03-22
  • 打赏
  • 举报
回复
System.OperatingSystem o = Environment.OSVersion;
MessageBox.Show(o.VersionString);
DinoSaur_1985 2012-03-22
  • 打赏
  • 举报
回复
我查了一下,你就判断他是不是nt6.1就可以了,截取字符串的时候改成-20就可以了。

Windows NT 6.1 = WIN7
Windows NT 6.0 = WINVISTA
Windows NT 5.1 = WINXP
Windows 4.9 = WINME
Windows NT 5.0 = WIN2K
Windows 4.1 = WIN98SE
Windows 4.01998 = WIN98
Windows 4.0 = WIN95
Windows 3.1 = WIN3.1
Windows 3.0 = WIN3.0
Windows 2.0 = WIN2.0
Windows 1.0 = WIN1.0
lanbing8023 2012-03-21
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 dinosaur_1985 的回复:]

你先用
System.OperatingSystem o = Environment.OSVersion;
MessageBox.Show(o.VersionString);

查看一下你WIN7的返回值是什么,然后 if(OSv== 返回值){}else{}

可能ServicePack的版本不同,那就处理一下字符串把ServicePack的信息去掉,然后在比较是不是相同。
[/Quote]这个怎么处理字符串把ServicePack的信息去掉,具体怎么做呢?
allen0118 2012-03-21
  • 打赏
  • 举报
回复
string a= Environment.OSVersion.ToString();

a=Microsoft Windows NT 5.1.2600 Service Pack 3
Teng_s2000 2012-03-21
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lanbing8023 的回复:]
谢谢您的回复,这个直接加在按钮下就可以么?我是刚入门C#,不要介意啊!
[/Quote]
可以的啊哈哈
DinoSaur_1985 2012-03-21
  • 打赏
  • 举报
回复

System.OperatingSystem o = Environment.OSVersion;

if (o.VersionString.Remove(o.VersionString.Length - 14).Trim() == "Microsoft Windows NT 5.1.2600")
MessageBox.Show("XP系统");
DinoSaur_1985 2012-03-21
  • 打赏
  • 举报
回复
你先用
System.OperatingSystem o = Environment.OSVersion;
MessageBox.Show(o.VersionString);

查看一下你WIN7的返回值是什么,然后 if(OSv== 返回值){}else{}

可能ServicePack的版本不同,那就处理一下字符串把ServicePack的信息去掉,然后在比较是不是相同。
加载更多回复(22)

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧