李国帅 编于2006-3-2 9:21
关于dotnet,不能在一般的控制台应用程序console中的静态main函数中使用带返回值的函数,因为这是还没有生成对象.
如
public static void Main() { string strtemp = GetNetCardMacAddress(); System.Console.WriteLine(strtemp); }
而应该编写类
using System.Management; namespace myDomain { class CTest { private ManagementClass mc; private ManagementObjectCollection moc; private ManagementObject disk; /// <summary> /// 取得设备网卡的MAC地址 /// </summary> public string GetNetCardMacAddress() { mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); moc = mc.GetInstances(); string str = ""; foreach(ManagementObject mo in moc) { if((bool)mo["IPEnabled"] == true) str = mo["MacAddress"].ToString(); } return str; } /// <summary> /// 取得设备硬盘的卷标号 /// </summary> /// <returns></returns> public string GetDiskVolumeSerialNumber() { disk = new ManagementObject("win32_logicaldisk.deviceid=\"c:\""); disk.Get(); return disk.GetPropertyValue("VolumeSerialNumber").ToString(); } } }
然后
public static void Main() { CTest run = new CTest(); System.Console.WriteLine("您的硬盘的卷标号" + run.GetDiskVolumeSerialNumber()); System.Console.WriteLine("您的网卡的" + run.GetNetCardMacAddress()); }