说明:本人准备写一些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());
}
}
}
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());
}
}
}
本文转自周金桥51CTO博客,原文链接:
http://blog.51cto.com/zhoufoxcn/164400
,如需转载请自行联系原作者