C#对window 硬件类操作,ManagementObjectSearcher

原文转载:http://blog.csdn.net/da_keng/article/details/50589145

纯属转载,复制过来方便编程时寻找。感谢作者:I-Awakening

复制前补充:

在刚学C#,用ManagementObjectSearcher 竟然不能解析到头文件,需要手动 Add Referance..



前言: 
我们在很多情况下想要获得计算机的硬件或操作系统的信息,比如获得CPU序列号给自己的软件添加机器码锁绑定指定电脑。又或者想要获得硬盘分区,声卡显卡等信息。

开篇:

我们用到的主要类是ManagementObjectSearcher,该类在System.Management命名空间下。 
有时候我们可以通过Environment获得一些简单的系统信息。 
如:Environment.MachineName;获得计算机名。 
Environment.UserName;获得操作系统登录用户名。 
不过在这篇文章中主要讨论ManagementObjectSearcher获取计算机硬件及操作系统的信息。

用法步骤:

  1. 添加引用:System.Management
  2. 引入命名空间:using System.Management;
  3. 创建ManagementObjectSearcher对象 
    anagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key); 
    其中的key见下面key列表:
  4. 通过searcher.Get()获得ManagementObjectCollection集合
  5. 遍历ManagementObjectCollection集合获得ManagementObject
  6. 通过managementObject[name]ManagementObject.GetPropertyValue(name)获得想要的属性 
    若不知道这里的name该写什么可以遍历打印一下:
foreach (var property in managementObject.Properties)
{
    Console.WriteLine(property.Name+":"+property.Value);
}

示例:

//获取CPU序列号
public string GetCPUSerialNumber()
{
    try
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Processor");
        string sCPUSerialNumber = "";
        foreach (ManagementObject mo in searcher.Get())
        {
            sCPUSerialNumber = mo["ProcessorId"].ToString().Trim();
            break;
        }
        return sCPUSerialNumber;
    }
    catch
    {
        return "";
    }
}
/获取主板序列号
public string GetBIOSSerialNumber()
{
    try
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_BIOS");
        string sBIOSSerialNumber = "";
        foreach (ManagementObject mo in searcher.Get())
        {
            sBIOSSerialNumber = mo.GetPropertyValue("SerialNumber").ToString().Trim();
            break;
        }
        return sBIOSSerialNumber;
    }
    catch
    {
        return "";
    }
}
//获取硬盘序列号
public string GetHardDiskSerialNumber()
{
    try
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");
        string sHardDiskSerialNumber = "";
        foreach (ManagementObject mo in searcher.Get())
        {
            sHardDiskSerialNumber = mo["SerialNumber"].ToString().Trim();
            break;
        }
        return sHardDiskSerialNumber;
    }
    catch
    {
        return "";
    }
}
//获取网卡地址
public string GetNetCardMACAddress()
{
    try
    {
        ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE ((MACAddress Is Not NULL) AND (Manufacturer <> 'Microsoft'))");
        string NetCardMACAddress = "";
        foreach (ManagementObject mo in searcher.Get())
        {
            NetCardMACAddress = mo["MACAddress"].ToString().Trim();
            break;
        }
        return NetCardMACAddress;
    }
    catch
    {
        return "";
    }
}

常用Key值:

ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from " + Key);

// 硬件 
Win32_Processor, // CPU 处理器 
Win32_PhysicalMemory, // 物理内存条 
Win32_Keyboard, // 键盘 
Win32_PointingDevice, // 点输入设备,包括鼠标。 
Win32_FloppyDrive, // 软盘驱动器 
Win32_DiskDrive, // 硬盘驱动器 
Win32_CDROMDrive, // 光盘驱动器 
Win32_BaseBoard, // 主板 
Win32_BIOS, // BIOS 芯片 
Win32_ParallelPort, // 并口 
Win32_SerialPort, // 串口 
Win32_SerialPortConfiguration, // 串口配置 
Win32_SoundDevice, // 多媒体设置,一般指声卡。 
Win32_SystemSlot, // 主板插槽 (ISA & PCI & AGP) 
Win32_USBController, // USB 控制器 
Win32_NetworkAdapter, // 网络适配器 
Win32_NetworkAdapterConfiguration, // 网络适配器设置 
Win32_Printer, // 打印机 
Win32_PrinterConfiguration, // 打印机设置 
Win32_PrintJob, // 打印机任务 
Win32_TCPIPPrinterPort, // 打印机端口 
Win32_POTSModem, // MODEM 
Win32_POTSModemToSerialPort, // MODEM 端口 
Win32_DesktopMonitor, // 显示器 
Win32_DisplayConfiguration, // 显卡 
Win32_DisplayControllerConfiguration, // 显卡设置 
Win32_VideoController, // 显卡细节。 
Win32_VideoSettings, // 显卡支持的显示模式。 

// 操作系统 
Win32_TimeZone, // 时区 
Win32_SystemDriver, // 驱动程序 
Win32_DiskPartition, // 磁盘分区 
Win32_LogicalDisk, // 逻辑磁盘 
Win32_LogicalDiskToPartition, // 逻辑磁盘所在分区及始末位置。 
Win32_LogicalMemoryConfiguration, // 逻辑内存配置 
Win32_PageFile, // 系统页文件信息 
Win32_PageFileSetting, // 页文件设置 
Win32_BootConfiguration, // 系统启动配置 
Win32_ComputerSystem, // 计算机信息简要 
Win32_OperatingSystem, // 操作系统信息 
Win32_StartupCommand, // 系统自动启动程序 
Win32_Service, // 系统安装的服务 
Win32_Group, // 系统管理组 
Win32_GroupUser, // 系统组帐号 
Win32_UserAccount, // 用户帐号 
Win32_Process, // 系统进程 
Win32_Thread, // 系统线程 
Win32_Share, // 共享 
Win32_NetworkClient, // 已安装的网络客户端 
Win32_NetworkProtocol, // 已安装的网络协议 

所有Key:

Win32_1394Controller
Win32_1394ControllerDevice
Win32_Account
Win32_AccountSID
Win32_ACE
Win32_ActionCheck
Win32_AllocatedResource
Win32_ApplicationCommandLine
Win32_ApplicationService
Win32_AssociatedBattery
Win32_AssociatedProcessorMemory
Win32_BaseBoard
Win32_BaseService
Win32_Battery
Win32_Binary
Win32_BindImageAction
Win32_BIOS
Win32_BootConfiguration
Win32_Bus
Win32_CacheMemory
Win32_CDROMDrive
Win32_CheckCheck
Win32_CIMLogicalDeviceCIMDataFile
Win32_ClassicCOMApplicationClasses
Win32_ClassicCOMClass
Win32_ClassicCOMClassSetting
Win32_ClassicCOMClassSettings
Win32_ClassInfoAction
Win32_ClientApplicationSetting
Win32_CodecFile
Win32_COMApplication
Win32_COMApplicationClasses
Win32_COMApplicationSettings
Win32_COMClass
Win32_ComClassAutoEmulator
Win32_ComClassEmulator
Win32_CommandLineAccess
Win32_ComponentCategory
Win32_ComputerSystem
Win32_ComputerSystemProcessor
Win32_ComputerSystemProduct
Win32_COMSetting
Win32_Condition
Win32_CreateFolderAction
Win32_CurrentProbe
Win32_DCOMApplication
Win32_DCOMApplicationAccessAllowedSetting
Win32_DCOMApplicationLaunchAllowedSetting
Win32_DCOMApplicationSetting
Win32_DependentService
Win32_Desktop
Win32_DesktopMonitor
Win32_DeviceBus
Win32_DeviceMemoryAddress
Win32_DeviceSettings
Win32_Directory
Win32_DirectorySpecification
Win32_DiskDrive
Win32_DiskDriveToDiskPartition
Win32_DiskPartition
Win32_DisplayConfiguration
Win32_DisplayControllerConfiguration
Win32_DMAChannel
Win32_DriverVXD
Win32_DuplicateFileAction
Win32_Environment
Win32_EnvironmentSpecification
Win32_ExtensionInfoAction
Win32_Fan
Win32_FileSpecification
Win32_FloppyController
Win32_FloppyDrive
Win32_FontInfoAction
Win32_Group
Win32_GroupUser
Win32_HeatPipe
Win32_IDEController
Win32_IDEControllerDevice
Win32_ImplementedCategory
Win32_InfraredDevice
Win32_IniFileSpecification
Win32_InstalledSoftwareElement
Win32_IRQResource
Win32_Keyboard
Win32_LaunchCondition
Win32_LoadOrderGroup
Win32_LoadOrderGroupServiceDependencies
Win32_LoadOrderGroupServiceMembers
Win32_LogicalDisk
Win32_LogicalDiskRootDirectory
Win32_LogicalDiskToPartition
Win32_LogicalFileAccess
Win32_LogicalFileAuditing
Win32_LogicalFileGroup
Win32_LogicalFileOwner
Win32_LogicalFileSecuritySetting
Win32_LogicalMemoryConfiguration
Win32_LogicalProgramGroup
Win32_LogicalProgramGroupDirectory
Win32_LogicalProgramGroupItem
Win32_LogicalProgramGroupItemDataFile
Win32_LogicalShareAccess
Win32_LogicalShareAuditing
Win32_LogicalShareSecuritySetting
Win32_ManagedSystemElementResource
Win32_MemoryArray
Win32_MemoryArrayLocation
Win32_MemoryDevice
Win32_MemoryDeviceArray
Win32_MemoryDeviceLocation
Win32_MethodParameterClass
Win32_MIMEInfoAction
Win32_MotherboardDevice
Win32_MoveFileAction
Win32_MSIResource
Win32_networkAdapter
Win32_networkAdapterConfiguration
Win32_networkAdapterSetting
Win32_networkClient
Win32_networkConnection
Win32_networkLoginProfile
Win32_networkProtocol
Win32_NTEventlogFile
Win32_NTLogEvent
Win32_NTLogEventComputer
Win32_NTLogEventLog
Win32_NTLogEventUser
Win32_ODBCAttribute
Win32_ODBCDataSourceAttribute
Win32_ODBCDataSourceSpecification
Win32_ODBCDriverAttribute
Win32_ODBCDriverSoftwareElement
Win32_ODBCDriverSpecification
Win32_ODBCSourceAttribute
Win32_ODBCTranslatorSpecification
Win32_OnBoardDevice
Win32_OperatingSystem
Win32_OperatingSystemQFE
Win32_OSRecoveryConfiguration
Win32_PageFile
Win32_PageFileElementSetting
Win32_PageFileSetting
Win32_PageFileUsage
Win32_ParallelPort
Win32_Patch
Win32_PatchFile
Win32_PatchPackage
Win32_PCMCIAController
Win32_Perf
Win32_PerfRawData
Win32_PerfRawData_ASP_ActiveServerPages
Win32_PerfRawData_ASPnet_114322_ASPnetAppsv114322
Win32_PerfRawData_ASPnet_114322_ASPnetv114322
Win32_PerfRawData_ASPnet_ASPnet
Win32_PerfRawData_ASPnet_ASPnetApplications
Win32_PerfRawData_IAS_IASAccountingClients
Win32_PerfRawData_IAS_IASAccountingServer
Win32_PerfRawData_IAS_IASAuthenticationClients
Win32_PerfRawData_IAS_IASAuthenticationServer
Win32_PerfRawData_InetInfo_InternetInformationServicesGlobal
Win32_PerfRawData_MSDTC_DistributedTransactionCoordinator
Win32_PerfRawData_MSFTPSVC_FTPService
Win32_PerfRawData_MSSQLSERVER_SQLServerAccessMethods
Win32_PerfRawData_MSSQLSERVER_SQLServerBackupDevice
Win32_PerfRawData_MSSQLSERVER_SQLServerBufferManager
Win32_PerfRawData_MSSQLSERVER_SQLServerBufferPartition
Win32_PerfRawData_MSSQLSERVER_SQLServerCacheManager
Win32_PerfRawData_MSSQLSERVER_SQLServerDatabases
Win32_PerfRawData_MSSQLSERVER_SQLServerGeneralStatistics
Win32_PerfRawData_MSSQLSERVER_SQLServerLatches
Win32_PerfRawData_MSSQLSERVER_SQLServerLocks
Win32_PerfRawData_MSSQLSERVER_SQLServerMemoryManager
Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationAgents
Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationDist
Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationLogreader
Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationMerge
Win32_PerfRawData_MSSQLSERVER_SQLServerReplicationSnapshot
Win32_PerfRawData_MSSQLSERVER_SQLServerSQLStatistics
Win32_PerfRawData_MSSQLSERVER_SQLServerUserSettable
Win32_PerfRawData_netFramework_netCLRExceptions
Win32_PerfRawData_netFramework_netCLRInterop
Win32_PerfRawData_netFramework_netCLRJit
Win32_PerfRawData_netFramework_netCLRLoading
Win32_PerfRawData_netFramework_netCLRLocksAndThreads
Win32_PerfRawData_netFramework_netCLRMemory
Win32_PerfRawData_netFramework_netCLRRemoting
Win32_PerfRawData_netFramework_netCLRSecurity
Win32_PerfRawData_Outlook_Outlook
Win32_PerfRawData_PerfDisk_PhysicalDisk
Win32_PerfRawData_Perfnet_Browser
Win32_PerfRawData_Perfnet_Redirector
Win32_PerfRawData_Perfnet_Server
Win32_PerfRawData_Perfnet_ServerWorkQueues
Win32_PerfRawData_PerfOS_Cache
Win32_PerfRawData_PerfOS_Memory
Win32_PerfRawData_PerfOS_Objects
Win32_PerfRawData_PerfOS_PagingFile
Win32_PerfRawData_PerfOS_Processor
Win32_PerfRawData_PerfOS_System
Win32_PerfRawData_PerfProc_FullImage_Costly
Win32_PerfRawData_PerfProc_Image_Costly
Win32_PerfRawData_PerfProc_JobObject
Win32_PerfRawData_PerfProc_JobObjectDetails
Win32_PerfRawData_PerfProc_Process
Win32_PerfRawData_PerfProc_ProcessAddressSpace_Costly
Win32_PerfRawData_PerfProc_Thread
Win32_PerfRawData_PerfProc_ThreadDetails_Costly
Win32_PerfRawData_RemoteAccess_RASPort
Win32_PerfRawData_RemoteAccess_RASTotal
Win32_PerfRawData_RSVP_ACSPerRSVPService
Win32_PerfRawData_Spooler_PrintQueue
Win32_PerfRawData_TapiSrv_Telephony
Win32_PerfRawData_Tcpip_ICMP
Win32_PerfRawData_Tcpip_IP
Win32_PerfRawData_Tcpip_NBTConnection
Win32_PerfRawData_Tcpip_networkInterface
Win32_PerfRawData_Tcpip_TCP
Win32_PerfRawData_Tcpip_UDP
Win32_PerfRawData_W3SVC_WebService
Win32_PhysicalMedia
Win32_PhysicalMemory
Win32_PhysicalMemoryArray
Win32_PhysicalMemoryLocation
Win32_PNPAllocatedResource
Win32_PnPDevice
Win32_PnPEntity
Win32_PointingDevice
Win32_PortableBattery
Win32_PortConnector
Win32_PortResource
Win32_POTSModem
Win32_POTSModemToSerialPort
Win32_PowerManagementEvent
Win32_Printer
Win32_PrinterConfiguration
Win32_PrinterController
Win32_PrinterDriverDll
Win32_PrinterSetting
Win32_PrinterShare
Win32_PrintJob
Win32_PrivilegesStatus
Win32_Process
Win32_Processor
Win32_ProcessStartup
Win32_Product
Win32_ProductCheck
Win32_ProductResource
Win32_ProductSoftwareFeatures
Win32_ProgIDSpecification
Win32_ProgramGroup
Win32_ProgramGroupContents
Win32_ProgramGroupOrItem
Win32_Property
Win32_ProtocolBinding
Win32_PublishComponentAction
Win32_QuickFixEngineering
Win32_Refrigeration
Win32_Registry
Win32_RegistryAction
Win32_RemoveFileAction
Win32_RemoveIniAction
Win32_ReserveCost
Win32_ScheduledJob
Win32_SCSIController
Win32_SCSIControllerDevice
Win32_SecurityDescriptor
Win32_SecuritySetting
Win32_SecuritySettingAccess
Win32_SecuritySettingAuditing
Win32_SecuritySettingGroup
Win32_SecuritySettingOfLogicalFile
Win32_SecuritySettingOfLogicalShare
Win32_SecuritySettingOfObject
Win32_SecuritySettingOwner
Win32_SelfRegModuleAction
Win32_SerialPort
Win32_SerialPortConfiguration
Win32_SerialPortSetting
Win32_Service
Win32_ServiceControl
Win32_ServiceSpecification
Win32_ServiceSpecificationService
Win32_SettingCheck
Win32_Share
Win32_ShareToDirectory
Win32_ShortcutAction
Win32_ShortcutFile
Win32_ShortcutSAP
Win32_SID
Win32_SMBIOSMemory
Win32_SoftwareElement
Win32_SoftwareElementAction
Win32_SoftwareElementCheck
Win32_SoftwareElementCondition
Win32_SoftwareElementResource
Win32_SoftwareFeature
Win32_SoftwareFeatureAction
Win32_SoftwareFeatureCheck
Win32_SoftwareFeatureParent
Win32_SoftwareFeatureSoftwareElements
Win32_SoundDevice
Win32_StartupCommand
Win32_SubDirectory
Win32_SystemAccount
Win32_SystemBIOS
Win32_SystemBootConfiguration
Win32_SystemDesktop
Win32_SystemDevices
Win32_SystemDriver
Win32_SystemDriverPNPEntity
Win32_SystemEnclosure
Win32_SystemLoadOrderGroups
Win32_SystemLogicalMemoryConfiguration
Win32_SystemMemoryResource
Win32_SystemnetworkConnections
Win32_SystemOperatingSystem
Win32_SystemPartitions
Win32_SystemProcesses
Win32_SystemProgramGroups
Win32_SystemResources
Win32_SystemServices
Win32_SystemSetting
Win32_SystemSlot
Win32_SystemSystemDriver
Win32_SystemTimeZone
Win32_SystemUsers
Win32_TapeDrive
Win32_TemperatureProbe
Win32_Thread
Win32_TimeZone
Win32_Trustee
Win32_TypeLibraryAction
Win32_UninterruptiblePowerSupply
Win32_USBController
Win32_USBControllerDevice
Win32_UserAccount
Win32_UserDesktop
Win32_VideoConfiguration
Win32_VideoController
Win32_VideoSettings
Win32_VoltageProbe
Win32_WMIElementSetting
Win32_WMISetting

  • 15
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
四知四清四握信息管理系统软件注册 编辑 | 删除 | 权限设置 | 更多▼ 更多▲ 设置置顶 推荐日志 转为私密日志 hamit 发表于2010年06月12日 16:15 阅读(2) 评论(0) 分: 个人日记 权限: 公开 Imports System.Management Imports System.IO Public Class tizimlitix Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click If Trim(TextBox2.Text) = "" Then MsgBox("你必须输入<注册码>", MsgBoxStyle.Exclamation) Exit Sub End If If Trim(TextBox2.Text) = Trim(TextBox1.Text) * 2 - 9950019 Then ' 存入(机器码) If File.Exists(Application.StartupPath + "\tizim_id.txt") Then File.Delete(Application.StartupPath + "\tizim_id.txt") End If Dim sr As StreamWriter = File.CreateText(Application.StartupPath + "\tizim_id.txt") sr.WriteLine(TextBox2.Text) sr.Close() '存入(注册码) If File.Exists(Application.StartupPath + "\pc_id.txt") Then File.Delete(Application.StartupPath + "\pc_id.txt") End If Dim sr2 As StreamWriter = File.CreateText(Application.StartupPath + "\Pc_id.txt") sr2.WriteLine(TextBox1.Text) sr2.Close() MsgBox("注册成功!程序将要重新启动!", MsgBoxStyle.Exclamation) Me.Close() End Else MsgBox("注册码不正确!请软件开发者联系 13899326632", MsgBoxStyle.Exclamation) End If End Sub Private Sub tizimlitix_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim cmicWmi As New System.Management.ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive") '获得硬盘序列号 2010-6-8 tordin izdap taptim hamdulla mahmut 13909950019 Dim Uint32 As UInt32 For Each cmicWmiObj As ManagementObject In cmicWmi.Get Uint32 = cmicWmiObj("signature") Next TextBox1.Text = Uint32.ToString * 2 - 13909950019 '本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/21aspnet/archive/2004/10/29/159124.aspx TextBox1.Enabled = False End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Me.Close() End Sub End Class
C#全能速查宝典》共分为8章,分别介绍了C#语言基础、Windows窗体及常用控件、Windows高级控件、控件公共属性、方法及事件、数据库开发、文件、数据流与注册表、GDI+绘图技术和C#高级编程,共包含562个C#编程中常用的属性、方法、和各种技术,每一个知识点都配有具体的示例,便于读者理解。 《C#全能速查宝典》所讲的知识点按照功能和字母进行排序,读者既可以按照功能顺序查找,又可以按照字母顺序学习。 《C#全能速查宝典》不仅适合C#程序设计初学者,也可作为中、高级程序开发人员的参考手册。 ============================================================ 图书目录 第1章 C#语言基础 1 1.1 常用概念、关键字及基础 1 1.1.1 abstract关键字——抽象 1 1.1.2 as操作符——引用型转换 3 1.1.3 base关键字——从派生中访问基的成员 3 1.1.4 变量——存储特定型的数据 4 1.1.5 Console——控制台中的输入流、输出流和错误流 6 1.1.6 Convert——型转换 8 1.1.7 常量——值不改变的量 9 1.1.8 Dispose方法——释放资源 10 1.1.9 迭代器——相同型的值的有序序列的一段代码 10 1.1.10 泛型——处理算法和数据结构 11 1.1.11 分部——将一个分成几部分 12 1.1.12 is操作符——检查变量是否为指定的型 14 1.1.13 lock关键字——锁定 15 1.1.14 namespace关键字——定义命名空间 15 1.1.15 new运算符——创建一个新的型实例 16 1.1.16 Object型——所有型的基 17 1.1.17 OOP技术——面向对象编程技术 18 1.1.18 ReadLine方法——从当前流中读取一行字符 20 1.1.19 typeof运算符——获得系统原型对象的型 21 1.1.20 using关键字——引入命名空间 22 1.1.21 WriteLine方法——写入流 23 1.2 数学方法——Math 25 1.2.1 Abs方法——返回指定数字的绝对值 25 1.2.2 Acos方法——返回余弦值为指定数字的角度 26 1.2.3 Asin方法——返回正弦值为指定数字的角度 26 1.2.4 Atan方法——返回正切值为指定数字的角度 27 1.2.5 Pow方法——返回指定数字的指定次幂 27 1.2.6 Round方法——将小数值舍入到指定的精度 28 1.3 流程控制语句 29 1.3.1 break语句——跳出循环 29 1.3.2 case语句——比较表达式以确定结果 30 1.3.3 continue语句——继续执行下一个循环 31 1.3.4 do…while语句——循环语句 31 1.3.5 for语句——循环语句 32 1.3.6 foreach语句——枚举一个集合的元素 33 1.3.7 goto语句——跳转到标签 34 1.3.8 if…else语句——条件判断语句 36 1.3.9 return语句——返回 38 1.3.10 switch case语句——条件判断语句 39 1.3.11 throw语句——显式引发异常 40 1.3.12 try…catch…finally语句——捕捉异常 42 1.3.13 while语句——循环语句 43 1.4 字符串处理 44 1.4.1 AddDays方法——添加天数 44 1.4.2 AddString方法——添加文本字符串 45 1.4.3 Compare方法——比较两个字符串 46 1.4.4 CompareTo方法——比较两个字符串对象 47 1.4.5 DATEADD函数——在指定日期上加一段时间 48 1.4.6 DateDiff方法——获取日期时间的间隔数 48 1.4.7 DateTime结构——表示时间上的一刻 50 1.4.8 DAY函数——返回日期部分的整数 51 1.4.9 DayOfWeek属性——获取星期几 52 1.4.10 Equals方法——比较两个字符串对象 53 1.4.11 First函数——返回查询结果的第一个记录 55 1.4.12 FirstDayOfWeek属性——获取或设置一周中的第一天 56 1.4.13 Format方法——格式化字符串 56 1.4.14 GETDATE函数——返回当前系统日期和时间 58 1.4.15 GetDayOfMonth方法——返回几号 59 1.4.16 GetDayOfWeek方法——返回星期几 59 1.4.17 GetDayOfYear方法——返回第几天 60 1.4.18 GetDaysInMonth方法——返回指定月份中的天数 60 1.4.19 GetDaysInYear方法——返回指定年份中的天数 61 1.4.20 GetMonth方法——返回指定日期中的月份 61 1.4.21 GetMonthsInYear方法——返回指定年份的月数 62 1.4.22 GetText方法——检索文本数据 63 1.4.23 GetYear方法——返回指定日期中的年份 64 1.4.24 IndexOf方法——确定指定字符在字符串中的索引 65 1.4.25 IsLeapYear方法——判断年份是否为闰年 67 1.4.26 IsMatch方法——搜索正则表达式匹配项 67 1.4.27 IsUpper方法——判断是否大写 68 1.4.28 Join方法——串联字符串 69 1.4.29 LastIndexOf方法——确定字符在字符串中最后索引 70 1.4.30 Matches方法——检查字符串是否有重复的词出现 71 1.4.31 MONTH函数——返回指定日期中月部分的整数 73 1.4.32 PadLeft方法——在左边用空格填充 73 1.4.33 PadRight方法——在右边用空格填充 74 1.4.34 Random——伪随机数生成器 75 1.4.35 Regex——正则表达式 76 1.4.36 Split方法——分割字符串 78 1.4.37 String——字符串 79 1.4.38 StringBuilder——可变字符串 82 1.4.39 Substring方法——截取字符串 83 1.4.40 TimeSpan对象——表示时间间隔或持续时间 84 1.4.41 ToInt32方法——转换为32位有符号整数 85 1.4.42 ToLongDateString 方法——转换为长日期字符串 86 1.4.43 ToLongTimeString 方法——转换为长时间字符串 87 1.4.44 ToLower方法——转换为小写 87 1.4.45 ToShortDateString方法——转换为短日期字符串 88 1.4.46 ToShortTimeString方法——转换为短时间字符串 88 1.4.47 ToString方法——转换为字符串 89 1.4.48 ToUpper方法——转换为大写 90 1.4.49 Trim方法——移除所有空白字符 91 1.4.50 TrimEnd方法——从尾部移除匹配项 92 1.4.51 TrimStart方法——从开始移除匹配项 92 1.4.52 YEAR函数——返回指定日期的年份的整数 93 1.5 数组与集合 93 1.5.1 Add方法——添加项 93 1.5.2 ArrayList——集合 95 1.5.3 AsEnumerable方法——转换为IEnumerable型 97 1.5.4 Clear方法——清空内容 98 1.5.5 Contains方法——确定是否包含某项 99 1.5.6 ContainsKey方法——确定哈希表是否包含特定键 100 1.5.7 ContainsText方法——确定剪贴板中是否存在数据 101 1.5.8 ContainsValue方法——确定哈希表是否包含特定值 101 1.5.9 Count属性——获取数目 102 1.5.10 GetEnumerator方法——循环访问对象 103 1.5.11 GetEnvironmentVariables方法——检索环境变量 104 1.5.12 Hashtable——哈希表 106 1.5.13 Insert方法——插入项 110 1.5.14 Item属性——获取或设置指定索引处的元素 111 1.5.15 Length属性——获取长度 112 1.5.16 Next方法——返回一个指定范围内的随机数 113 1.5.17 Queue——队列 115 1.5.18 Remove方法——移除指定项 116 1.5.19 RemoveAt方法——移除指定索引处的项 118 1.5.20 Replace方法——替换文件或字符串 119 1.5.21 Reverse方法——反转数组元素 120 1.5.22 Sort方法——数组排序 121 1.5.23 Stack——堆栈 123 第2章 Windows窗体及常用控件 126 2.1 Form窗体 126 2.1.1 AcceptButton属性——设置接受按钮 126 2.1.2 Activate事件——当激活窗体时发生 126 2.1.3 Appllication——提供管理应用程序的静态方法 126 2.1.4 CancelButton属性——设置取消按钮 128 2.1.5 Computer——提供操作计算机组件的属性 129 2.1.6 ComputerInfo——获取计算机信息 130 2.1.7 Control——定义控件基 131 2.1.8 Environment——提供当前环境和平台的信息 134 2.1.9 Form窗体——可视化界面 136 2.1.10 FormClosed事件——关闭窗体后事件 139 2.1.11 FormClosing事件——关闭窗体前事件 139 2.1.12 Icon属性——设置图标 139 2.1.13 IsMdiContainer属性——设置父窗体 140 2.1.14 LayoutMdi方法——排列子窗体 141 2.1.15 Load事件——窗体加载事件 141 2.1.16 MaximizeBox属性——是否显示最大化按钮 142 2.1.17 Maximum属性——设置数字显示框的最大值 142 2.1.18 MDI窗体——多文档界面 143 2.1.19 MdiChildren属性——获取子窗体的数组 146 2.1.20 MdiParent属性——设置父窗体 147 2.1.21 MinimizeBox属性——是否显示最小化按钮 147 2.1.22 Minimum属性——数字显示框的最小值 148 2.1.23 Opacity属性——设置窗体的透明度级别 148 2.1.24 Owner属性——设置窗体所有者 149 2.1.25 StartPosition属性——设置窗体起始位置 150 2.1.26 StartupPath 属性——获取可执行文件路径 150 2.1.27 TopMost属性——窗体是否应显示为最顶层窗体 151 2.1.28 WindowState属性——窗体的窗口状态 151 2.2 文本控件 152 2.2.1 AllowEdit属性——是否可以编辑列表项 152 2.2.2 AppendText方法——追加文本 152 2.2.3 BeginEdit方法——将单元格置于编辑模式下 153 2.2.4 Button控件——按钮控件 153 2.2.5 CancelEdit属性——取消更改 155 2.2.6 CanPaste方法——是否可以粘贴数据 155 2.2.7 CanRedo属性——是否有可以重新应用的操作 156 2.2.8 CanSelect属性——是否可以选中控件 157 2.2.9 CanUndo属性——能否撤销上一个操作 157 2.2.10 Cut方法——将选定内容移动到“剪贴板”中 158 2.2.11 Find方法——搜索指定的项目 158 2.2.12 FindString方法——搜索文本 160 2.2.13 Label控件——标签控件 161 2.2.14 LabelEdit属性——允许用户编辑控件数据 163 2.2.15 LinkLabel控件——以超链接形式显示文本 164 2.2.16 MaskedTextBox控件——使用掩码区分用户输入 166 2.2.17 Multiline属性——是否为多行输入数据 169 2.2.18 PasswordChar属性——取代用户输入而显示的字符 170 2.2.19 Redo方法——重新应用控件中上次撤销的操作 171 2.2.20 RichTextBox控件——有格式文本控件 171 2.2.21 Select方法——激活控件 173 2.2.22 SelectAll方法——选定所有文本 176 2.2.23 Selected属性——是否选定 176 2.2.24 SelectedCells属性——用户选定的单元格集合 177 2.2.25 SelectedColumns属性——用户选定的列集合 178 2.2.26 SelectedRows属性——用户选定的行集合 179 2.2.27 SelectionBackColor属性——文本在选中时的颜色 180 2.2.28 SelectionColor属性——插入点的文本颜色 180 2.2.29 SelectionEnd属性——设置选定日期范围的结束日期 181 2.2.30 SelectionFont属性——选定文本或插入点的字体 182 2.2.31 SelectionIndent属性——所选内容开始行的缩进距离 183 2.2.32 SelectionLength属性——控件中选定的字符数 184 2.2.33 SelectionRange 属性——设置选定的日期范围 185 2.2.34 SelectionStart属性——选择的起始位置的字符索引 185 2.2.35 TextBox控件——输入或显示文本 186 2.2.36 TextChanged事件——Text属性值更改时发生 187 2.3 选择控件 188 2.3.1 CheckBox控件——复选框控件 188 2.3.2 CheckBoxes属性——是否显示复选框 190 2.3.3 Checked属性——复选框是否处于选中状态 190 2.3.4 CheckedChanged事件——Checked属性更改时发生 191 2.3.5 CheckedListBox控件——复选框列表控件 191 2.3.6 CheckState属性——设置CheckBox控件的状态 193 2.3.7 ComboBox控件——下拉组合框控件 194 2.3.8 DomainUpDown控件——上下选择控件 195 2.3.9 DropDownStyle属性——指定组合框样式的值 197 2.3.10 GetItemCheckState方法——当前项的复选状态的值 198 2.3.11 GetItemText方法——指定项的文本表示形式 199 2.3.12 Index属性——从零开始的索引 200 2.3.13 Items属性——数组列表对象中的项的集合 200 2.3.14 ListBox控件——列表控件 201 2.3.15 ListView控件——显示带图标的项列表 205 2.3.16 NumericUpDown控件——数值选择控件 208 2.3.17 RadioButton控件——单选按钮 210 2.3.18 SelectedIndex属性——获取选择项的索引 212 2.3.19 SelectedIndices属性——表示当前选中的项 213 2.3.20 SelectedItem属性——当前选中的项 214 2.3.21 SelectedItems属性——选定项的集合 215 2.3.22 SelectedText属性——选定文本 216 2.4 容器控件 217 2.4.1 FlatStyle属性——设置控件的平面样式外观 217 2.4.2 FlowDirection属性——指示FlowLayoutPanel控件的流向 217 2.4.3 FlowLayoutPanel控件——水平或垂直排列内容 218 2.4.4 GroupBox控件——分组控件 219 2.4.5 Panel控件——容器控件 220 2.4.6 TabControl控件——选项卡控件 222 2.4.7 TabIndex属性——控件的Tab键顺序 224 2.4.8 TabPages属性——选项卡页的集合 224 第3章 Windows高级控件 226 3.1 日期时间控件 226 3.1.1 CalendarFont属性——日历的字体样式 226 3.1.2 CalendarForeColor属性——日历的前景色 226 3.1.3 DateTimePicker控件——日期和日历的组合 226 3.1.4 MaxDate属性——最大日期和时间 228 3.1.5 MinDate属性——最小日期和时间 228 3.1.6 MonthCalendar控件——以网格形式显示日历 229 3.1.7 SetDate方法——将日期设置为当前选定的日期 231 3.1.8 ShowToday属性——是否显示当前日期 232 3.2 对话框、菜单、工具栏及状态栏控件 232 3.2.1 ColorDialog控件——颜色对话框 232 3.2.2 ContextMenuStrip控件——右键快捷菜单 233 3.2.3 ExpandAll方法——展开所有树节点 233 3.2.4 Filter属性——设置筛选器字符串 234 3.2.5 FolderBrowserDialog控件——浏览文件夹对话框 234 3.2.6 Font属性——设置字体 235 3.2.7 FontDialog控件——字体对话框 235 3.2.8 InitialDirectory属性——文件对话框显示的初始目录 237 3.2.9 MenuStrip控件——菜单控件 238 3.2.10 Nodes属性——树节点集合 241 3.2.11 OpenFileDialog控件——打开文件对话框 241 3.2.12 RestoreDirectory属性——是否还原当前目录 244 3.2.13 RootFolder属性——设置浏览的根文件夹 245 3.2.14 SaveFileDialog组件——保存文件对话框 246 3.2.15 SelectedNode属性——获取选定的树节点 248 3.2.16 SelectedPath属性——用户选定的路径 249 3.2.17 ShowDialog方法——打开模式对话框 249 3.2.18 ToolStrip控件——工具栏控件 251 3.2.19 TreeNode——树节点 252 3.2.20 TreeView控件——树控件 254 3.3 数据绑定控件 256 3.3.1 BindingNavigator控件——导航和操作数据 256 3.3.2 Cell对象——表示Word文档中的单元格 258 3.3.3 CellClick事件——单元格的任何部分被单击时发生 259 3.3.4 CellEnter事件——控件接收到输入焦点时发生 260 3.3.5 CellMouseClick事件——鼠标单击单元格时发生 261 3.3.6 CellLeave事件——单元格失去输入焦点时发生 261 3.3.7 Cells属性——Bookmark控件中的表单元格 261 3.3.8 ColumnCount属性——DataGridView控件显示的列数 262 3.3.9 Columns属性——控件中所有列的集合 262 3.3.10 ColumnWidth属性——ListBox中列的宽度 263 3.3.11 CurrentCell属性——设置当前处于活动状态的单元格 263 3.3.12 CurrentRow属性——包含当前单元格的行 263 3.3.13 DataGridView控件——数据控件 264 3.3.14 FullRowSelect属性——是否选择其所有子项 268 3.3.15 GetCellCount方法——获取满足筛选器的单元格数目 269 3.3.16 GetColumn方法——指定子控件的列位置 270 3.3.17 NewRow方法——添加一条新记录 270 3.3.18 RowCount方法——DataGridView中显示的行数 271 3.3.19 Rows属性——DataGridView控件中的所有行 272 3.4 打印控件 273 3.4.1 CrystalReportViewer控件——水晶报表查看控件 273 3.4.2 Document属性——设置要预览的文档 280 3.4.3 PageSetupDialog组件——配置页面的对话框 281 3.4.4 Print方法——打印当前页面 283 3.4.5 PrintDialog组件——打印对话框 283 3.4.6 PrintDocument组件——设置打印的文档 286 3.4.7 PrinterSettings属性——打印机设置 291 3.4.8 PrintPage事件——当需要为当前页打印的输出时发生 292 3.4.9 PrintPreviewControl组件——按文档打印时的外观显示Print Document组件 292 3.4.10 PrintPreviewDialog组件——显示PrintDocument组件在打印时的外观 295 3.4.11 PrinterSettings——用来指定有关文档打印方式的信息 297 3.4.12 Zoom属性——指示页面的显示大小 300 3.5 其他常用组件 300 3.5.1 BackgroundWorker组件——在主线程的另一线程上异步执行耗时的操作 300 3.5.2 ErrorProvider控件——检查并显示错误信息 302 3.5.3 EventLog组件——连接本地和远程计算机的事件日志 303 3.5.4 HelpProvider组件——将帮助文件与Windows应用程序相关联 306 3.5.5 HScrollBar控件——一个标准Windows水平滚动条 309 3.5.6 Image属性——显示在控件上的图像 311 3.5.7 ImageAlign属性——在控件中显示的图像的对齐方式 312 3.5.8 ImageFormat——指定图像的格式 312 3.5.9 ImageList组件——用于存储图像 314 3.5.10 ImageList属性——在控件中显示的图像的ImageList 316 3.5.11 Interval属性——设置Timer控件执行的间隔 317 3.5.12 NotifyIcon控件——设置程序的系统托盘图标 317 3.5.13 PerformStep方法——按照Step属性的数量增加进度栏的当前位置 319 3.5.14 PictrueBox控件——用于显示指定的图像 320 3.5.15 Play方法——播放.wav文件 323 3.5.16 ProgressBar控件——进度条 323 3.5.17 SetError方法——设置错误信息 326 3.5.18 SetShowHelp方法——是否显示帮助信息 327 3.5.19 SetToolTip方法——设置提示文本 328 3.5.20 Step属性——增加进度条的当前位置时所根据的数量 328 3.5.21 Stop方法——停止加载网页 329 3.5.22 Tick事件——计时器处于启用状态时发生 330 3.5.23 Timer组件——定期引发事件的组件 330 3.5.24 ToolTip控件——显示提示信息 332 3.5.25 ToolTipIcon属性——提示文本旁显示的图标型 333 3.5.26 ToolTipText属性——ToolTip显示的文本 334 3.5.27 ToolTipTitle属性——工具提示窗口的标题 334 3.5.28 TrackBar控件——标准的Windows跟踪条 335 3.5.29 Url属性——引用服务说明的URL 337 3.5.30 VscrollBar控件——标准的Windows垂直滚动条 337 3.5.31 WebBrowser控件——在窗体中显示网页 339 3.5.32 Windows Media Player控件——播放常见的音频文件 343 第4章 控件公共属性、方法及事件 347 4.1 控件公共属性 347 4.1.1 BackColor属性——设置控件的背景色 347 4.1.2 BackgroudColor属性——设置控件背景色 347 4.1.3 BackgroudImage属性——设置控件背景图像 347 4.1.4 Border属性——控件边框 348 4.1.5 BorderStyle属性——控件的边框样式 349 4.1.6 Bottom属性——控件下边缘与其容器的工作区上边缘之间的距离 349 4.1.7 CanFocus属性——控件是否可以接收焦点 350 4.1.8 Capture属性——控件是否已捕获鼠标 350 4.1.9 Color属性——设置用户选定的颜色 350 4.1.10 Dock属性——控件在窗体中的布局样式 351 4.1.11 Enabled属性——控件是否可用 352 4.1.12 ForeColor属性——设置控件的前景色 352 4.1.13 Handle属性——获取控件绑定到的窗口句柄 352 4.1.14 Height属性——设置控件的高度 353 4.1.15 KeyChar属性——设置与按下的键对应的字符 354 4.1.16 KeyValue属性——获取KeyDown或KeyUp事件的键盘值 355 4.1.17 Lines属性——设置多行配置中的文本行 355 4.1.18 Location属性——控件的左上角相对于其容器的左上角的坐标 356 4.1.19 Name属性——控件或实例的名称 356 4.1.20 Parent属性——设置控件的父容器或获取指定子目录的父目录 357 4.1.21 Position属性——设置坐标 358 4.1.22 ReadOnly属性——是否只读 359 4.1.23 Right属性——控件右边缘与其容器的工作区左边缘之间的距离 359 4.1.24 RightToLeft属性——控件的文本从右向左读取 360 4.1.25 ScrollBars属性——滚动条的可见性和位置 360 4.1.26 SizeMode属性——指示如何显示图像 361 4.1.27 Tag属性——窗体或控件的标识 362 4.1.28 Text属性——与控件关联的文本 362 4.1.29 TextAlign 属性——控件上文本的对齐方式 363 4.1.30 Top属性——控件上边缘与其容器的工作区上边缘之间的距离 364 4.1.31 Value属性——辅助性对象的值 364 4.1.32 View属性——项在控件中的显示方式 365 4.1.33 Visible属性——控件是否可见 366 4.1.34 Width属性——控件的宽度 366 4.2 控件公共方法 367 4.2.1 BringToFront方法——将控件带到Z顺序的前面 367 4.2.2 Focus方法——为控件设置输入焦点 367 4.2.3 GetClipboardContent方法——检索选定单元格内容的格式化值 368 4.2.4 GetParent方法——检索指定路径的父目录 368 4.2.5 Hide方法——隐藏窗体 369 4.2.6 Load方法——加载XML文档 369 4.2.7 LoadFile方法——将文件加载到RichTextBox控件中 371 4.2.8 Navigate方法——打开指定的URL地址 372 4.2.9 Refresh方法——重新加载当前的网页 373 4.2.10 SaveAs方法——用新名称或新格式保存文档 373 4.2.11 SaveFile方法——将内容保存到文件中 374 4.2.12 Show方法——显示光标或者打开新窗体 375 4.2.13 UpButton方法——按照指定数值递增 376 4.3 控件公共事件 377 4.3.1 Click事件——单击控件时触发该事件 377 4.3.2 Enter事件——光标进入控件时发生 378 4.3.3 KeyDown事件——控件有焦点按下键时发生 378 4.3.4 KeyPress事件——控件有焦点按下键时发生 380 4.3.5 KeyUp事件——控件有焦点释放键时发生 381 4.3.6 Leave事件——输入焦点离开控件时发生 381 4.3.7 MouseClick事件——用户单击控件时发生 382 4.3.8 Navigated事件——加载新文档时发生 383 4.3.9 Paint事件——重绘或更新控件时发生 383 第5章 数据库开发 385 5.1 SQL语言基础 385 5.1.1 AVG聚合函数——返回组中值的平均值 385 5.1.2 CAST函数——数据型显式转换 385 5.1.3 COUNT函数——返回组中的项的数量 386 5.1.4 Last函数——返回查询结果的最后一个记录 386 5.1.5 MAX函数——返回表达式中的最大值 388 5.1.6 MIN函数——返回表达式中的最小值 388 5.1.7 newid函数——创建uniqueidentifier型的惟一值 389 5.1.8 SUM函数——返回表达式中所有值的和 389 5.1.9 UPDATE语句——更改表中的现有数据 390 5.2 ADO.NET技术 392 5.2.1 Command对象——对数据源执行增、删、改、查操作 392 5.2.2 CommandText属性——获取设置SQL语句或存储过程 393 5.2.3 CommandTimeout属性——获取或设置错误等待时间 393 5.2.4 CommandType属性——获取或设置如何解释CommandText属性 394 5.2.5 Connection对象——数据库连接对象 394 5.2.6 ConnectionState枚举——数据库连接状态 395 5.2.7 DataAdapter——数据库桥接器 396 5.2.8 DataMember属性——获取或设置数据源列表或表名称 398 5.2.9 DataReader——只读数据集 398 5.2.10 DataSet——数据集 400 5.2.11 DataSource属性——获取或设置数据源 402 5.2.12 ExecuteNonQuery方法——执行SQL语句并返回受影响的行数 402 5.2.13 ExecuteReader方法——执行SQL语句并返回DataReader对象 403 5.2.14 ExecuteScalar方法——执行SQL语句并返回结果集中第1行的第1列 404 5.2.15 Fill方法——填充数据集 405 5.2.16 Merge方法——合并数据集 407 5.2.17 Parameters属性——获取SqlParameterCollection 409 5.2.18 ReadXml方法——将XML架构和数据读入数据集 410 5.2.19 SelectCommand属性——获取或设置选择记录命令 411 5.2.20 SQL注入式攻击——利用设计上的漏洞攻击SQL 412 5.2.21 SqlCommand——SQL执行命令 413 5.2.22 SqlConnection——SQL数据库连接对象 415 5.2.23 SqlDataAdapter——SQL数据库桥接器 416 5.2.24 SqlDataReader——SQL只读数据集 418 5.2.25 Tables属性——获取包含在数据集中的表的集合 421 5.2.26 Update方法——使控件重绘工作区内的无效区域 422 5.2.27 UpdateCommand属性——获取或设置更新记录命令 423 5.2.28 WriteXml方法——将数据集中数据写入到XML中 423 5.3 LINQ技术 424 5.3.1 Lambda表达式——匿名函数 424 5.3.2 LINQ技术——语言集成查询 426 5.3.3 LinqToDataSet技术——LINQ操作数据集 427 5.3.4 LinqToObjects技术——LINQ操作数组和集合 429 5.3.5 LinqToSql技术——LINQ操作SQL数据库 431 5.3.6 LinqToXml技术——LINQ操作XML文件 436 5.3.7 var关键字——根据初始化语句推断变量型 439 第6章 文件、数据流与注册表 441 6.1 文件与I/O数据流 441 6.1.1 ASCII码——键盘的一种表示方式 441 6.1.2 ASCIIEncoding——ASCII字符编码的操作 442 6.1.3 Attributes属性——获取和设置文件的属性 443 6.1.4 BinaryReader——将特定的数据读作二进制值 445 6.1.5 BinaryWriter——将二进制值写入到流中 447 6.1.6 CanRead属性——判断当前流是否支持读写 448 6.1.7 Close方法——释放所有关联的资源 449 6.1.8 Copy方法——文件的复制 450 6.1.9 CopyFile方法——将文件复制到新的位置 451 6.1.10 CopyTo方法——将指定的字符串复制到字符数组中 452 6.1.11 Create方法——创建文件 455 6.1.12 CreateDirectory方法——创建指定路径中的所有目录 456 6.1.13 CreateText方法——创建或打开文本文件 456 6.1.14 CreationTime属性——获取或设置文件的创建时间 457 6.1.15 CryptoStream——将数据流连接到加密转换的流 457 6.1.16 Delete方法——删除文件 461 6.1.17 Directory——对文件夹进行操作 463 6.1.18 DirectoryEntry——封装节点或对象 464 6.1.19 DirectoryInfo——对文件夹进行操作 466 6.1.20 DirectoryName属性——获取路径 468 6.1.21 DirectorySearcher组件——执行查找 468 6.1.22 DriveInfo——驱动器的信息访问 469 6.1.23 Encoding属性——获取编码方式 470 6.1.24 Exists方法——判断文件是否存在 471 6.1.25 Exists属性——判断文件是否存在 472 6.1.26 Extension属性——获取文件扩展名 473 6.1.27 File——对文件进行操作 473 6.1.28 FileAttributes枚举——提供文件和目录的属性 475 6.1.29 FileInfo——文件的操作 476 6.1.30 FileName属性——获取或设置文件的名称 478 6.1.31 FileStream——对文件流操作 478 6.1.32 Flush方法——清除流的缓冲区 480 6.1.33 GetBytes方法——将字符串编码设为字节序列 481 6.1.34 GetDirectories方法——获取子目录的名称 482 6.1.35 GetExtension方法——获取路径字符串的扩展名 485 6.1.36 GetFiles方法——获取目录中的文件名称 486 6.1.37 GetFileSystemEntries方法——获取目录中的所有名称 487 6.1.38 GetFileSystemInfos方法——获取所有文件的信息 489 6.1.39 GetStream方法——返回用于发送和接收的数据 491 6.1.40 GetString方法——将字节解码成字符串 491 6.1.41 HasRows属性——指示 OleDbDataReader是否有数据 493 6.1.42 MD5CryptoServiceProvider——操作MD5的 493 6.1.43 MemoryStream——创建其支持存储区为内存的流 495 6.1.44 Move方法——文件的移动 497 6.1.45 MoveNext方法——移动到下一个字符 497 6.1.46 MoveTo方法——文件的移动 498 6.1.47 NetworkStream——网络访问的基础数据流 500 6.1.48 Open方法——打开文件 502 6.1.49 OpenFile方法——以只读方式打开文件 503 6.1.50 OpenText方法——打开UTF-8编码文本文件 504 6.1.51 Path属性——监视的目录的路径 505 6.1.52 Peek方法——返回下一个可用的字符 506 6.1.53 Read方法——读取数据流 507 6.1.54 ReadBytes方法——将指定的字节读入字节数组 508 6.1.55 ReadToEnd方法——从流的当前位置读到末尾 509 6.1.56 Stream——对数据流进行操作 510 6.1.57 StreamReader——数据流的读取 512 6.1.58 StreamWriter——数据流的写入 513 6.1.59 TextReader——读取连续字符的读取器 515 6.1.60 TextWriter——编写一个有序字符系列的编写器 516 6.1.61 Write方法——将流写入到文件中 517 6.2 注册表技术 521 6.2.1 CreateSubKey方法——创建或打开子项 521 6.2.2 GetValue方法——获取注册表项中的值 522 6.2.3 GetValueNames方法——所有值名称的字符串数组 523 6.2.4 GetSubKeyNames方法——所有子项名称字符串数组 525 6.2.5 OpenSubKey方法——以只读方式检索子项 525 6.2.6 Registry——注册表操作 528 6.2.7 RegistryKey——表示Windows注册表中的项级节点 529 6.2.8 SetValue方法——设置注册表项的指定名称/值对 531 第7章 GDI+绘图技术 532 7.1 GDI+绘图基础 532 7.1.1 Bitmap——图像对象 532 7.1.2 Cursor——绘制光标指针图像 533 7.1.3 GDI+——图形图像的绘制 535 7.1.4 Graphics——绘图 536 7.1.5 GraphicsPath——一系列相互连接的直线和曲线 540 7.1.6 Icon——图标的操作 542 7.1.7 Image——图像的操作 543 7.1.8 LinearGradientBrush——线性渐变封装Brush 545 7.1.9 Region——由矩形和路径构成的图形形状的内部 547 7.1.10 SolidBrush——定义单色画笔 548 7.2 常用绘图方法 549 7.2.1 Draw方法——绘制光标 549 7.2.2 DrawArc方法——绘制圆弧 550 7.2.3 DrawBezier方法——绘制贝塞尔样条 551 7.2.4 DrawEllipse方法——绘制椭圆 553 7.2.5 DrawImage方法——绘制Image图像 555 7.2.6 DrawLine方法——绘制直线 556 7.2.7 DrawPath方法——绘制GraphicsPath图形路径 558 7.2.8 DrawPie方法——绘制扇形 558 7.2.9 DrawPolygon方法——绘制多边形 560 7.2.10 DrawRectangle方法——绘制矩形 561 7.2.11 DrawString方法——绘制文本字符串 562 7.3 常用填充图像方法 565 7.3.1 FillEllipse方法——填充椭圆 565 7.3.2 FillPath方法——填充GraphicsPath的内部 566 7.3.3 FillPie方法——填充扇形 567 7.3.4 FillPolygon方法——填充多边形 568 7.3.5 FillRectangle方法——填充矩形框 570 7.3.6 FillRegion方法——填充一个区域 572 7.4 其他常用方法 572 7.4.1 Clone方法——创建Bitmap对象的某个部分的副本 572 7.4.2 CreateGraphics方法——创建Graphics对象 574 7.4.3 FromArgb方法——从ARGB值创建Color结构 574 7.4.4 FromFile方法——从指定的文件创建Image 577 7.4.5 FromImage方法——从Image创建新的Graphics对象 578 7.4.6 FromStream方法——数据流创建Image 578 7.4.7 GetPixel方法——获取图像中的像素颜色 580 7.4.8 GetThumbnailImage方法——Image的缩略图 581 7.4.9 Save方法——将图片以文件的形式进行复制 583 7.4.10 SetPixel方法——设置图像中的像素颜色 583 7.4.11 Transform方法——对路径的数据点进行变换 584 第8章 C#高级编程 586 8.1 网络编程技术 586 8.1.1 Accept方法——为新建连接创建新的Socket对象 586 8.1.2 AcceptSocket方法——接收挂起的连接请求 586 8.1.3 BeginConnect方法——开始远程主机连接的异步请求 587 8.1.4 Dns——从Internet域名系统检索特定主机的信息 588 8.1.5 GetHostAddresses方法——返回主机的IP地址 589 8.1.6 GetHostByAddress方法——创建IPHostEntry实例 590 8.1.7 GetHostByName方法——获取指定DNS主机名的信息 591 8.1.8 GetHostName方法——获取本地计算机的主机名 592 8.1.9 IPEndPoint——将网络端点表示为IP地址和端口号 592 8.1.10 IPHostEntry——为主机地址信息提供容器 594 8.1.11 Listen方法——将Socket置于侦听状态 596 8.1.12 MachineName属性——读取或写入事件的计算机名称 596 8.1.13 MailMessage——邮件的操作 597 8.1.14 Net send命令——用net send命令进行发送 598 8.1.15 Net use命令——实现映射网络驱动器 599 8.1.16 Ping——网络访问远程计算机的操作 601 8.1.17 POP3协议——POP邮件的操作 603 8.1.18 Receive方法——由远程主机发送的UDP数据报 608 8.1.19 Send方法——将数据发送到连接的Socket 609 8.1.20 SerialPort——控制串行端口文件资源 610 8.1.21 SMTP协议——进行邮件的传输 612 8.1.22 SmtpClient——将电子邮件发送到SMTP服务器 614 8.1.23 Socket——网络通信的操作 616 8.1.24 TcpClient——为TCP网络服务提供客户端连接 618 8.1.25 TcpListener——从TCP网络客户端侦听连接 619 8.1.26 UdpClient——用户数据报(UDP)网络服务 620 8.1.27 WebClient——URI标识的资源发送和接收 623 8.1.28 WebRequest——访问Internet数据 625 8.1.29 WebResponse——协议特定的响应 629 8.2 多线程编程 630 8.2.1 Abort方法——终止线程 630 8.2.2 BeginInvoke方法——线程上异步执行委托 631 8.2.3 EndInvoke方法——异步操作的返回值 632 8.2.4 Join方法——确保线程已终止 633 8.2.5 Kill方法——强制关闭进程 633 8.2.6 Process——对正在计算机上运行的进程的访问 635 8.2.7 Sleep方法——线程挂起 640 8.2.8 Start方法——启动进程 640 8.2.9 Thread——创建并控制线程的 642 8.2.10 ThreadState属性——获取当前线程的状态 645 8.3 WMI技术——系统管理 646 8.3.1 MainWindowTitle属性——获取进程的主窗口标题 646 8.3.2 ManagementClass——公共信息模型管理 647 8.3.3 ManagementObject——表示WMI实例 648 8.3.4 ManagementObjectSearcher——查询检索管理对象 650 8.3.5 ManagementScope——管理操作的范围 651 8.3.6 Microsoft.Win32命名空间——操作注册表 652 8.3.7 WndProc方法——处理Windows消息 654 8.4 其他高级技术 655 8.4.1 Children属性——获取节点的子项 655 8.4.2 COM+服务——为的实例提供服务 655 8.4.3 DirectShow技术——流媒体处理的一个开发包 656 8.4.4 DLL组件——动态链接库 663 8.4.5 MVC开发模式——模型视图控制器 664 8.4.6 VFW技术——视频应用程序提供的软件工具包 666 8.4.7 XML——定义其他标识语言的元标识语言 668 8.4.8 XmlReader——XML读取器 670 8.4.9 XmlWriter——XML编写器 673 附录——字母索引 676
C#中编写自动设备硬件,需要使用.NET Framework提供的System.Management命名空间,该命名空间包含用于管理Windows系统硬件操作系统的。 下面是一个简单的示例,演示如何使用System.Management命名空间编写自动设备硬件: ```csharp using System.Management; public class Hardware { private ManagementObjectSearcher searcher; public Hardware(string query) { searcher = new ManagementObjectSearcher(query); } public ManagementObjectCollection GetHardwareInfo() { return searcher.Get(); } } ``` 在上面的代码中,我们定义了一个名为Hardware的,它接受一个字符串参数query,该参数是设备硬件信息的WMI查询字符串。然后,我们在的构造函数中创建了一个ManagementObjectSearcher对象,并将查询字符串作为参数传递。最后,我们定义了一个GetHardwareInfo方法,该方法返回一个ManagementObjectCollection对象,其中包含了查询结果中的所有硬件信息。 使用该时,可以按照以下方式进行调用: ```csharp Hardware hardware = new Hardware("SELECT * FROM Win32_Processor"); ManagementObjectCollection hardwareInfo = hardware.GetHardwareInfo(); foreach (ManagementObject obj in hardwareInfo) { Console.WriteLine("Processor: " + obj["Name"]); } ``` 在上面的代码中,我们首先创建一个Hardware对象,并将WMI查询字符串"SELECT * FROM Win32_Processor"作为参数传递。然后,我们调用GetHardwareInfo方法,获取查询结果的ManagementObjectCollection对象。最后,我们使用foreach循环遍历ManagementObjectCollection对象,并输出每个处理器的名称。 当然,这只是一个简单的示例,您可以根据需要编写更复杂的自动设备硬件,以满足您的应用程序的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值