您的位置:首页 > 编程语言 > C#

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: