如何获得 WINDOWS 版本[C# 版]
2024-07-21 02:18:18
供稿:网友
using system;
using system.runtime.interopservices;
namespace getwindowsversion
{
[ structlayout( layoutkind.sequential )]
public class osversioninfo
{
public int osversioninfosize;
public int majorversion;
public int minorversion;
public int buildnumber;
public int platformid;
[ marshalas( unmanagedtype.byvaltstr, sizeconst=128 )]
public string versionstring;
}
[ structlayout( layoutkind.sequential )]
public struct osversioninfo2
{
public int osversioninfosize;
public int majorversion;
public int minorversion;
public int buildnumber;
public int platformid;
[ marshalas( unmanagedtype.byvaltstr, sizeconst=128 )]
public string versionstring;
}
public class libwrap
{
[ dllimport( "kernel32" )]
public static extern bool getversionex( [in, out] osversioninfo osvi );
[ dllimport( "kernel32", entrypoint="getversionex" )]
public static extern bool getversionex2( ref osversioninfo2 osvi );
}
public class app
{
public static void main()
{
console.writeline( "/npassing osversioninfo as class" );
osversioninfo osvi = new osversioninfo();
osvi.osversioninfosize = marshal.sizeof( osvi );
libwrap.getversionex( osvi );
console.writeline( "class size: {0} /noperation system : {1}/npack: {2}", osvi.osversioninfosize, opsysname(osvi.majorversion, osvi.minorversion,osvi.platformid),osvi.versionstring);
console.writeline("{0}", osvi.platformid);
console.writeline( "/npassing osversioninfo as struct" );
osversioninfo2 osvi2 = new osversioninfo2();
osvi2.osversioninfosize = marshal.sizeof( osvi2 );
libwrap.getversionex2( ref osvi2 );
console.writeline( "static size: {0} /noperation system : {1}/npack: {2}", osvi2.osversioninfosize, opsysname(osvi2.majorversion, osvi2.minorversion,osvi2.platformid),osvi2.versionstring);
}
public static string opsysname(int majorversion,int minorversion ,int platformid)
{
string str_opn =string.format("{0}.{1}",majorversion,minorversion);
switch(str_opn)
{
case "4.0":
return win95_nt40(platformid);
case "4.10":
return "windows 98";
case "4.90":
return "windows me";
case "3.51":
return "windows nt 3.51";
case "5.0":
return "windwos 2000";
case "5.1":
return "windwos xp";
case "5.2":
return "windows server 2003 family";
default:
return "this windows version is not distinguish!";
}
}
public static string win95_nt40(int platformid)
{
switch(platformid)
{
case 1:
return "windows 95";
case 2:
return "windows nt 4.0";
default:
return "this windows version is not distinguish!";
}
}
}
}