没有按要求用c-free写对话框程序,因为布希饭c-free的界面和大写字母。不过写控制台应用程序更快更吃皮~
C语言和C#中都是用GetLogicalDrives()方法,所以没多大区别。只是在C#中有Environment空间,更方便获取系统信息。
C代码:(二进制码的移动用了昨天看的《编程之美》里的方法)
- #include "windows.h"
- #include "stdio.h"
- int main()
- {
- //本机所有磁盘
- DWORD DWdisk=GetLogicalDrives();
- char disk[256];
- itoa(DWdisk,disk,2);
- puts(disk);
- //算法发现:位移
- int Count(DWORD v)
- {
- int num=0;
- while(v)
- {
- v&=v-1;
- num++;
- }
- return num;
- }
- //本机磁盘个数
- printf("本机共有%d逻辑磁盘。/n", Count(DWdisk));
- //DRIVE_NO_ROOT_DIR:无效盘符
- if (GetDriveType("Z:") != DRIVE_NO_ROOT_DIR)
- printf("有一堆Z盘/n");
- else
- printf("摸得Z盘得!/n");
- return 0;
- }
C#代码:
- /*
- * Created by SharpDevelop.
- * User: Sconfield
- * Date: 2009/9/2
- * Time: 16:46
- *
- * To change this template use Tools | Options | Coding | Edit Standard Headers.
- */
- using System;
- namespace ComputerEnvironment
- {
- class Disk
- {
- public static void Main()
- {
- #region 磁盘信息
- //本机操作系统版本
- Console.WriteLine("My Operating System:{0}",Environment.OSVersion);
- //此文件路径
- Console.WriteLine("我在"+Environment.CurrentDirectory);
- //本机所有磁盘(包括虚拟光驱)
- bool diskD=false;
- string[] drives=Environment.GetLogicalDrives();
- for(int i=0;i<drives.Length;i++)
- {
- Console.WriteLine("磁盘{0}: {1} ", i+1, drives[i]);
- //判断是否存在G盘
- if(drives[i]==@"D:/")
- { diskD=true; }
- }
- if(diskD)
- {Console.WriteLine("有一个D盘");}
- //堆栈信息
- Console.WriteLine(Environment.StackTrace);
- //系统启动后经过的时间(不是启动时间)
- Console.WriteLine("时间:"+Environment.TickCount);
- #endregion
- #region 圆点面积
- Console.WriteLine("请输入圆的半径:");
- double radius=Convert.ToDouble(Console.ReadLine());
- Console.WriteLine("半径为{0}的园面积为:{1}", radius, Area(radius));
- #endregion
- }
- /// <summary>
- /// 圆点面积
- /// </summary>
- /// <param name="rad">半径</param>
- /// <returns>返回面积</returns>
- private static double Area(double rad)
- {
- //返回的时候没有用变量来储存“圆的面积”,这样可以避免溢出吗?
- //系统默认的数据类型貌似也是double
- return Math.PI*Math.Pow(rad,2);
- }
- }
- }