C#小技巧系列之一:获取系统有关信息
2007-05-07 21:32
447 查看
说明:本人准备写一些C#有关的小技巧系列文章,这些文章含金量并不高,代码难度不大,不过因为问的次数比较多,从而导致本人决定用自己所知的方式写这一系列文章,可以看做“趣味导学”系列吧。
要展示的第一个例子就是获取系统磁盘情况和操作系统名称,获取的信息包括本机上所有磁盘盘符,磁盘类型(软驱、硬盘分区、光盘),磁盘文件类型(FAT32/NSFS),磁盘空间总大小及剩余空间大小等。为了方便查看代码,用控制台式编写。代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace LocalDriveInfo
...{
class Program
...{
static void Main(string[] args)
...{
string[] drives = System.Environment.GetLogicalDrives();
DriveInfo driveInfo = null;
System.Console.WriteLine(GetOperationSystemInName());
System.Console.WriteLine("卷标 盘符 类型 文件系统 总大小(byte) 可用空间大小(byte)");
for (int i = 0; i < drives.Length; i++)
...{
driveInfo = new DriveInfo(drives[i]);
if (driveInfo.IsReady)
...{
System.Console.WriteLine("{0} {1} {2} {3} {4} {5}", driveInfo.VolumeLabel, driveInfo.Name,
driveInfo.DriveType, driveInfo.DriveFormat, driveInfo.TotalSize.ToString("###,###") /**//*/ (1024 * 1024)*/, driveInfo.AvailableFreeSpace.ToString("###,###") /**//*/ (1024*1024))*/);
}
else
...{
System.Console.WriteLine("{0} {1} {2} {3} {4} {5}", "", driveInfo.Name,
driveInfo.DriveType, "", "", "", "");
}
}
System.Console.ReadLine();
}
/**///// <summary>
/// 获取系统名称
/// </summary>
/// <returns></returns>
public static string GetOperationSystemInName()
...{
OperatingSystem os = System.Environment.OSVersion;
string osName = "UNKNOWN";
switch (os.Platform)
...{
case PlatformID.Win32Windows:
switch (os.Version.Minor)
...{
case 0: osName = "Windows 95"; break;
case 10: osName = "Windows 98"; break;
case 90: osName = "Windows ME"; break;
}
break;
case PlatformID.Win32NT:
switch (os.Version.Major)
...{
case 3: osName = "Windws NT 3.51"; break;
case 4: osName = "Windows NT 4"; break;
case 5: if (os.Version.Minor == 0)
...{
osName = "Windows 2000";
}
else if (os.Version.Minor == 1)
...{
osName = "Windows XP";
}
else if (os.Version.Minor == 2)
...{
osName = "Windows Server 2003";
}
break;
case 6: osName = "Longhorn"; break;
}
break;
}
return String.Format("{0},{1}", osName, os.Version.ToString());
}
}
}本文出自 “周公(周金桥)的专栏” 博客,请务必保留此出处http://zhoufoxcn.blog.51cto.com/792419/164400
要展示的第一个例子就是获取系统磁盘情况和操作系统名称,获取的信息包括本机上所有磁盘盘符,磁盘类型(软驱、硬盘分区、光盘),磁盘文件类型(FAT32/NSFS),磁盘空间总大小及剩余空间大小等。为了方便查看代码,用控制台式编写。代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace LocalDriveInfo
...{
class Program
...{
static void Main(string[] args)
...{
string[] drives = System.Environment.GetLogicalDrives();
DriveInfo driveInfo = null;
System.Console.WriteLine(GetOperationSystemInName());
System.Console.WriteLine("卷标 盘符 类型 文件系统 总大小(byte) 可用空间大小(byte)");
for (int i = 0; i < drives.Length; i++)
...{
driveInfo = new DriveInfo(drives[i]);
if (driveInfo.IsReady)
...{
System.Console.WriteLine("{0} {1} {2} {3} {4} {5}", driveInfo.VolumeLabel, driveInfo.Name,
driveInfo.DriveType, driveInfo.DriveFormat, driveInfo.TotalSize.ToString("###,###") /**//*/ (1024 * 1024)*/, driveInfo.AvailableFreeSpace.ToString("###,###") /**//*/ (1024*1024))*/);
}
else
...{
System.Console.WriteLine("{0} {1} {2} {3} {4} {5}", "", driveInfo.Name,
driveInfo.DriveType, "", "", "", "");
}
}
System.Console.ReadLine();
}
/**///// <summary>
/// 获取系统名称
/// </summary>
/// <returns></returns>
public static string GetOperationSystemInName()
...{
OperatingSystem os = System.Environment.OSVersion;
string osName = "UNKNOWN";
switch (os.Platform)
...{
case PlatformID.Win32Windows:
switch (os.Version.Minor)
...{
case 0: osName = "Windows 95"; break;
case 10: osName = "Windows 98"; break;
case 90: osName = "Windows ME"; break;
}
break;
case PlatformID.Win32NT:
switch (os.Version.Major)
...{
case 3: osName = "Windws NT 3.51"; break;
case 4: osName = "Windows NT 4"; break;
case 5: if (os.Version.Minor == 0)
...{
osName = "Windows 2000";
}
else if (os.Version.Minor == 1)
...{
osName = "Windows XP";
}
else if (os.Version.Minor == 2)
...{
osName = "Windows Server 2003";
}
break;
case 6: osName = "Longhorn"; break;
}
break;
}
return String.Format("{0},{1}", osName, os.Version.ToString());
}
}
}本文出自 “周公(周金桥)的专栏” 博客,请务必保留此出处http://zhoufoxcn.blog.51cto.com/792419/164400
相关文章推荐
- C#小技巧系列之一:获取系统有关信息
- C#小技巧系列之三:获取系统进程信息
- C#小技巧系列之三:获取系统进程信息
- C#小技巧系列之二:获取系统所有服务信息
- C#小技巧系列之三:获取系统进程信息
- C#小技巧系列之二:获取系统所有服务信息
- C#小技巧系列之二:获取系统所有服务信息
- C#小技巧系列之四:获取本地网络信息
- C#小技巧系列之四:获取本地网络信息
- C#小技巧系列之四:获取本地网络信息
- 用C#获取系统有关环境、属性
- VB中使用WMI获取系统硬件和软件有关信息
- ASP.NET(c#)运用API函数获取系统信息
- VB中使用WMI获取系统硬件和软件有关信息
- C#获取系统版本信息
- C#编程获取系统日志信息
- 关于C#中用WMI获取系统信息问题
- C# 获取机器的硬件信息(CPU ID序列号, 主板信息,硬盘序列号,系统信息)
- 【C#】获取系统信息
- C#获取当前计算机的硬件及系统信息