VBnet 读写注册表与获取系统信息

2008实例:读写注册表与获取系统信息
Visual Studio2019正式发布后吸引了大量的编程人员,为了由原来的Visual Studio2005转换为最新的Visual Studio2019不少公司也重新配置了计算器硬件设备,从而得以正常平稳的使用最新的开发工具。

Visual Studio2019在一些特性上也做出了许多的改进,访问注册表以及系统内核也变得更为方便了。

本文主要以Visual Studio2008 当中的语言介绍两实用的列子。

打开Visual Studio 2008在文件(File) 菜单上,单击新建项目(New Project)。

在新建项目(New Project) 对话框的模板(Templates) 窗格中,单击
Windows应用程序(Windows Application)。

单击确定(OK) 如图1。

图1
选择Form1窗体,在Form1窗体中添加如下控件:
3个按钮控件分别为:Button1、Button2、Button3、一个Textbox1控件、Label1控件
控件属性设置如下:
在这里我们以修改IE标题为实例,来读取与修改注册表当中的值,控件添加完后,界面进行适当的调整。

如图2
图2
在访问注册表的时候我们需要使用Registry 类,以下是Registry 类的详细信息
名称:Registry 类
CurrentUser
存储有关用户首选项的信息。

LocalMachine
存储本地计算器的配置信息。

ClassesRoot
存储有关类型(和类)及其属性的信息。

Users
存储有关默认用户配置的信息。

PerformanceData
存储软件组件的性能信息。

CurrentConfig
存储非用户特定的硬件信息。

DynData
存储动态数据。

Registry 类还包含从注册表项设置和检索值的staticGetValue 和SetValue 方法。

每次使用这两种方法时,它们都会打开和关闭注册表项,所以,当访问大量值时,还可以使用RegistryKey 类中的类似方法。

RegistryKey 类还提供了可用来为注册表项设置Windows 访问控制安全性的方法、可用来在检索值之前测试值的数据类型的方法以及删除项的方法。

访问注册表时我们还需要用到RegistryKey.OpenSubKey 方法
RegistryKey.OpenSubKey 方法(String, RegistryKeyPermissionCheck, RegistryRights)
进入代码编辑窗口
进入Button1_Click事件
进入Button2_Click事件
读写注册表的代码已经输入完毕了,接下来我们怎样获取系统信息呢?要获取系统信息我们需要使用SystemInformation 类。

有关SystemInformation 类的详细信息如下:
先为大家介绍SystemInformation 类的BootMode枚举,我们这个实例主要以BootMode枚举为例。

SystemInformation 类的BootMode枚举
SystemInformation 成员的属性非常关键大家需要了解相关的属性等信息(包括了大量获取系统信息一些方法)。

SystemInformation 成员的属性
进入代码编辑器模式
进入Button3_Click事件
好了代码输入完成,接下来我们需要进行程序测试了,在文本框中输入你想要输入的信息如图3
图3
我们可以打开IE浏览器看一下效果:
图4
随后我们可以单击<获取系统启动方式与信息>按钮,测试结果如图5所示:
图5
好了以上所有功能运行成功,这两个实例非常的实用,希望大家能够多去了解有关这方面的知识,万变不离其中。

合集下载

用VB操作注册表4

用VB操作注册表4

用VB操作注册表(四)梦里水乡登录数据库Registry的Value的存取--读取某个Key下的所有名称的值(Value)有时候我们需要读取某个Key下的所有名称的值,找到特定的或者全部名称的值以作它用,比如我在编写清除著名的"木马冰河" 服务器端程序时,就需要查找HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run下的所有可疑的加载程序项目然后删除之。

要用到的API函数RegEnumValue,其详细描述:VB声明Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Byte, lpcbData As Long) As Long'以下的两个函数是经过王国荣老师改编过的函数,与此相关,也一并列出.并且在我们的例子程序中要用到它们。

RegEnumValueAsAny可以传入长整数和字符串;RegEnumValueAsAny2中lpData参数被改成Any后,可以使用Byte数组,由于Byte数组是采用”传地址方式来传递参数的,可以省下复制字符串数据的时间,使得程序变得更加高效。

Declare Function RegEnumValueAsAny Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As LongDeclare Function RegEnumValueAsAny2 Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, lpValueName As Any, lpcbValueName As Long, lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long参数说明:hKey:Key HandledwIndex:欲读取之名称的顺序lpValueName:返回所读取的名称lpcbValueName:传入lpValueName参数的长度,返回所读取的名称的长度,注意这一长度不含chr(0)lpReserved:保留参数,实际使用时传入ByVal 0即可lpType:返回所读取的数据类型lpData:返回所读取的数据lpcbData:传入lpData,返回所读取的数据长度返回值: =0,表示成功;≠0,表示失败。

使用 Visual Basic .NET 访问注册表

使用 Visual Basic .NET 访问注册表

前提条件
要掌握本文内容,必须具备以下前提条件:
1. 熟悉以前版本的 Visual Basic。
2. 掌握有关注册表的设计和用途的知识。
3. 理解访问注册表的安全问题。
--------------------------------------------------------------------------------
"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\TestApp"
--------------------------------------------------
使用注册表编辑器访问注册表
当使用部署项目时,还可以选择使用注册表编辑器来指定要添加到目标计算机的注册表中的注册表项和项值。有关详细信息,请参阅 Registry Editor。
Registry 类提供基本的注册表项,用于访问子项及其值。基项本身是只读的。下表列出并说明了 Registry 类中提供的七个项。
下面的示例显示了如何从 HKEY_CURRENT_USER 中读取一个 DWORD 值:
Imports Microsoft.Win32
Dim regVersion As RegistryKey
' 不存在该项,创建该项。
regVersion =
Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\TestApp\\1.0")
End If
Dim intVersion As Integer = 0
If (Not regVersion Is Nothing) Then

VB取得系统信息

VB取得系统信息

这学期开VB,为了熟悉VB的环境,我决定写一个简单的东西来熟悉VB的编程环境.不过写了一点的时候就发现需要用到API函数才能完成程序所完成的功能,所以又学习以下API函数的使用(主要是关于注册标的操作).下面就如何在VB下利用API函数获得系统信息的方法,希望对大家的学习有所帮助.首先是获得当前用户的用户名,用的是API函数GetComputerName ,该函数在VB中的声明是:Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long (注意:VB中API函数的声明必须在一行上,还有窗体层内定义的API函数都应该定义成Private 的,不然编译是就会出错.另外,顺便说一下,VB虽然也支持面向对象的编程,但只能是实现面向对象的部分功能).其参数说明如下:lpBuffer String,随同计算机名载入的字串缓冲区;nSize Long,缓冲区的长度。

这个变量随同返回计算机名的实际长度载入;其实现过成如下:Dim len As Long 'computer 名字的长度Dim aa As Long '返回函数调用是否成功,TRUE(非零)表示成功,否则返回零Dim cmprName As String '计算机名(当前用户的用户名)的字符串cmprName = String(255, 0)len = 256aa = GetComputerName(cmprName, len) '取得这台计算机的名称cmprName = Left(cmprName, InStr(1, cmprName, Chr(0)) - 1) cname.Caption = cmprName '将计算机当前用户名输出在cname标签上(以下都是用标签做输出).接着就是CPU的信息了.在API函数中好象没有直接关于CPU的信息(虽然我对API函数知道的有现,但我查了一下,没有,不知道是不是有我没有找到).既然没有直接关于CPU信息的API函数,我就想到注册表里面肯定有CPU的相关信息.我查了一下注册表,发现有关CPU的信息都在键"HKEY_LOCAL_MACHINE\HARDWARE\DESCRIPTION\system\CentralProce ssor\0"下,关于CPU有的型号在"ProcessorNameString"里面.接下来就是如何读取注册表的值了.VB中用RegOpenKey和RegQueryValueEx两个api来完成读取注册表的值.其中RegOpenKey是打开注册表中一个指定的键;RegQueryValueEx函数来读取某个Key的指定名称的值(value),这两个函数的声明及参数说明如下:Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long参数类型及说明:hKey:Key HandlelpSubKey:SubKey名称或路径phkResult:若RegOpenKey执行成功,则这一参数返回Subkey的hKey.返回值: =0,表示成功;≠0,表示失败。

批处理操作注册表完全攻略(读取注册表写入注册表等)

批处理操作注册表完全攻略(读取注册表写入注册表等)

批处理操作注册表完全攻略(读取注册表写⼊注册表等)⼀,批处理⽣成.Reg⽂件操作注册表 ⽤批处理中的重定向符号可以轻松地⽣成.reg⽂件。

然后⽤命令执⾏.reg⽂件即可! 这⾥,着重要了解.reg⽂件操作注册表的⽅法。

⾸先.reg⽂件⾸⾏必须是:Windows Registry Editor Version 5.00。

然后才是操作注册表的内容。

(就和从注册表中导出的⽂件格式⼀致) 1,创建⼦项 Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\TTT] 在HKEY_LOCAL_MACHINE\SOFTWARE\下创建了⼀个名字为“TTT”的⼦项。

2,创建⼀个项⽬名称复制代码代码如下: Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\TTT] "Name"="TTT BLOG" "EMail"="taoether@" "URL"="/ttt/" "Type"=dword:02 这样就在[HKEY_LOCAL_MACHINE\SOFTWARE\TTT]下新建了:Name、EMail、 URL、Type这四个项⽬ Name、Email、URL的类型是“String Value” Type的类型是“DWORD Value” (附:windows注册表值类型: REG_SZ 字符串值 REG_BINARY ⼆进制值 REG_DWORD DWORD值 REG_MULTI_SZ 多字符串值 REG_EXPAND_SZ 可扩充字符串值) 3,修改键值 修改相对来说⽐较简单,只要把你需要修改的项⽬导出,然后⽤记事本进⾏修改,然后导⼊(regedit /s)即可。

VB利用API和注册表获取系统信息下

VB利用API和注册表获取系统信息下

VB利用API和注册表获取系统信息下VB利用API和注册表获取系统信息下x:=getsystemmetrics(SM_ARRANGE);file://获取系统最小化窗口的起始位置和排列方式y:=x;x:=x and 3;y:=y and 12;case x ofARW_BOTTOMLEFT : buffer:=' 最小化窗口:自左下角'; ARW_BOTTOMRIGHT : buffer:=' 最小化窗口:自右下角'; ARW_TOPLEFT : buffer:=' 最小化窗口:自左上角';ARW_TOPRIGHT : buffer:=' 最小化窗口:自右上角';end;case y ofARW_LEFT : buffer:=buffer+'横向排列';ARW_UP : buffer:=buffer+'纵向排列';ARW_HIDE : buffer:=buffer+'隐藏';end;memo1.lines.add(buffer);getmem(computername,255); file://获取计算机名称和用户名getmem(username,255);i:=255;getcomputername(computername,i);memo1.lines.add(' 计算机名称: '+computername); getusername(username,i);memo1.lines.add(' 用户名: '+username);freemem(computername);freemem(username);getsysteminfo(cpuinfo); file://获取CPU类型case cpuinfo.dwProcessorType of386 : buffer:=' CPU类型: 386';486 : buffer:=' CPU类型: 486';586 : buffer:=' CPU类型: Pentium';end;memo1.Lines.add(buffer);file://从注册表中获取CPU标识,Windows版本,产品标识,注册单位名称及用户名myreg:=Tregistry.Create;myreg.RootKey:=HKEY_LOCAL_MACHINE;ifmyreg.OpenKey('hardware\descriptionsystem\centralprocessor\ 0',false) then memo1.lines.add(' CPU标识:'+myreg.ReadString('VendorIdentifier'));myreg.closekey;if myreg.openkey('software\microsoft\ windows\currentversion',false) then beginmemo1.lines.add(' windows版本: '+myreg.ReadString('Version'));memo1.lines.add(' 版本号: '+myreg.ReadString('VersionNumber')+''+myreg.ReadString('Subversionnumber'));memo1.lines.add(' 产品标识: '+myreg.Readstring('ProductID'));memo1.lines.add('注册单位名称: '+myreg.readstring('RegisteredOrganization'));memo1.lines.add(' 注册用户名: '+myreg.readstring('RegisteredOwner')); end;myreg.CloseKey;myreg.Free;end; end。

VB注册表读写模块

VB注册表读写模块

VB注册表读写模块'使用示例:'新建串值'SetStringValue "HKEY_LOCAL_MACHINE", "String Value", "Hello Visual Basic programmer"'SetStringValue"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Curre ntVersion\Run", "Internat", "internat.exe"'新建二进制值'SetBinaryValue "HKEY_LOCAL_MACHINE", "Binary Value", Chr$(&H1) + Chr$(&H2) + Chr$(&H3) + Chr$(&H4)'新建 DWORD 值'SetDWORDValue "HKEY_LOCAL_MACHINE", "DWORD Value", "1"'读取串chuan值'GetStringValue("HKEY_LOCAL_MACHINE", "String Value")'读取二进制值'GetBinaryValue("HKEY_LOCAL_MACHINE", "Binary Value") 'If rtn = Chr$(&H1) + Chr$(&H2) + Chr$(&H3) + Chr$(&H4) Then'读取 DWORD 值'GetDWORDValue("HKEY_LOCAL_MACHINE", "DWORDValue")'删除键值'DelValue("HKEY_LOCAL_MACHINE", "String Value")'新建主键'CreateKey "HKEY_LOCAL_MACHINE\Registry Editor"'删除主键'DeleteKey "HKEY_LOCAL_MACHINE\Registry Editor"Type FILETIMElLowDateTime As LonglHighDateTime As LongEnd TypePrivate Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _(ByVal hKey As Long, _ByVal lpSubKey As String, _ByVal ulOptions As Long, _ByVal samDesired As Long, phkResult As Long) As LongPrivate Declare Function RegCloseKey Lib "advapi32.dll" _(ByVal hKey As Long) As LongPrivate Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _(ByVal hKey As Long, _ByVal lpSubKey As String, phkResult As Long) As LongPrivate Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _(ByVal hKey As Long, _ByVal lpSubKey As String) As LongPrivate Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _(ByVal hKey As Long, _ByVal lpValueName As String, _ByVal lpReserved As Long, lpType As Long, _ByVal lpData As String, lpcbData As Long) As LongPrivate Declare Function RegQueryValueExA Lib "advapi32.dll" _(ByVal hKey As Long, _ByVal lpValueName As String, _ByVal lpReserved As Long, lpType As Long, _ByRef lpData As Long, lpcbData As Long) As LongPrivate Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _(ByVal hKey As Long, _ByVal lpValueName As String, _ByVal Reserved As Long, _ByVal dwType As Long, _ByVal lpData As String, _ByVal cbData As Long) As LongPrivate Declare Function RegSetValueExA Lib "advapi32.dll" _ (ByVal hKey As Long, _ByVal lpValueName As String, _ByVal Reserved As Long, _ByVal dwType As Long, _ByRef lpData As Long, _ByVal cbData As Long) As LongPrivate Declare Function RegSetValueExB Lib "advapi32.dll"Alias "RegSetValueExA" _(ByVal hKey As Long, _ByVal lpValueName As String, _ByVal Reserved As Long, _ByVal dwType As Long,_ByRef lpData As Byte, _ByVal cbData As Long) As LongPrivate Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _(ByVal hKey As Long, _ByVal lpValueName As String) As LongConst ERROR_SUCCESS = 0&Const ERROR_BADDB = 1009&Const ERROR_BADKEY = 1010&Const ERROR_CANTOPEN = 1011&Const ERROR_CANTREAD = 1012&Const ERROR_CANTWRITE = 1013&Const ERROR_OUTOFMEMORY = 14&Const ERROR_INVALID_PARAMETER = 87&Const ERROR_ACCESS_DENIED = 5&Const ERROR_NO_MORE_ITEMS = 259&Const ERROR_MORE_DATA = 234&Const REG_NONE = 0&Const REG_SZ = 1&Const REG_EXPAND_SZ = 2&Const REG_BINARY = 3&Const REG_DWORD = 4&Const REG_DWORD_LITTLE_ENDIAN = 4&Const REG_DWORD_BIG_ENDIAN = 5&Const REG_LINK = 6&Const REG_MULTI_SZ = 7&Const REG_RESOURCE_LIST = 8&Const REG_FULL_RESOURCE_DESCRIPTOR = 9&Const REG_RESOURCE_REQUIREMENTS_LIST = 10&Const KEY_QUERY_VALUE = &H1&Const KEY_SET_VALUE = &H2&Const KEY_CREATE_SUB_KEY = &H4&Const KEY_ENUMERATE_SUB_KEYS = &H8&Const KEY_NOTIFY = &H10&Const KEY_CREATE_LINK = &H20&Const READ_CONTROL = &H20000Const WRITE_DAC = &H40000Const WRITE_OWNER = &H80000Const SYNCHRONIZE = &H100000Const STANDARD_RIGHTS_REQUIRED = &HF0000Const STANDARD_RIGHTS_READ = READ_CONTROLConst STANDARD_RIGHTS_WRITE = READ_CONTROLConst STANDARD_RIGHTS_EXECUTE = READ_CONTROLConst KEY_READ = STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFYConst KEY_WRITE = STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE Or KEY_CREATE_SUB_KEYConst KEY_EXECUTE = KEY_READDim hKey As Long, MainKeyHandle As LongDim rtn As Long, lBuffer As Long, sBuffer As StringDim lBufferSize As LongDim lDataSize As LongDim ByteArray() As Byte'This constant determins wether or not to display error messages to the'user. I have set the default value to False as an error message can and'does become irritating after a while. Turn this value to true if you want'to debug your programming code when reading and writing to your system'registry, as any errors will be displayed in a message box.Const DisplayErrorMsg = FalseFunction SetDWORDValue(SubKey As String, Entry As String, Value As Long)Call ParseKey(SubKey, MainKeyHandle)If MainKeyHandle Thenrtn = RegCreateKey(MainKeyHandle, SubKey, hKey) 'open the keyIf rtn = ERROR_SUCCESS Then 'if the key was open successfully thenrtn = RegSetValueExA(hKey, Entry, 0, REG_DWORD, Value, 4) 'write the valueIf Not rtn = ERROR_SUCCESS Then 'if there was an error writting the valueIf DisplayErrorMsg = True Then 'if the user want errors displayedMsgBox ErrorMsg(rtn) 'display the errorEnd IfEnd Ifrtn = RegCloseKey(hKey) 'close the keyElse 'if there was an error opening the keyIf DisplayErrorMsg = True Then'if the user want errors displayedMsgBox ErrorMsg(rtn) 'display the errorEnd IfEnd IfEnd IfEnd FunctionFunction GetDWORDValue(SubKey As String, Entry As String) Call ParseKey(SubKey, MainKeyHandle)If MainKeyHandle Thenrtn = RegCreateKey(MainKeyHandle, SubKey, hKey) 'open the keyIf rtn = ERROR_SUCCESS Then 'if the key could be opened thenrtn = RegQueryValueExA(hKey, Entry, 0, REG_DWORD, lBuffer, 4) 'get the value from the registryIf rtn = ERROR_SUCCESS Then 'if the value could be retreived thenrtn = RegCloseKey(hKey) 'close the keyGetDWORDValue = lBuffer 'return the valueElse 'otherwise, if the value couldnt be retreivedGetDWORDValue = "Error" 'return Error to the userIf DisplayErrorMsg = True Then 'if the user wants errors displayedMsgBox ErrorMsg(rtn) 'tell the user what was wrongEnd IfEnd IfElse 'otherwise, if the key couldnt be openedGetDWORDValue = "Error" 'return Error to the userIf DisplayErrorMsg = True Then 'if the user wants errors displayedMsgBox ErrorMsg(rtn) 'tell the user what was wrongEnd IfEnd IfEnd IfEnd Function'第二部分Function SetBinaryValue(SubKey As String, Entry As String, Value As String)Call ParseKey(SubKey, MainKeyHandle)If MainKeyHandle Thenrtn = RegCreateKey(MainKeyHandle, SubKey, hKey) 'open the keyIf rtn = ERROR_SUCCESS Then 'if the key was open successfully thenlDataSize = LenB(StrConv(Value, vbFromUnicode))ReDim ByteArray(lDataSize)For I = 1 To lDataSizeByteArray(I) = Asc(Mid$(Value, I, 1))Nextrtn = RegSetValueExB(hKey, Entry, 0, REG_BINARY, ByteArray(1), lDataSize) 'write the valueIf Not rtn = ERROR_SUCCESS Then 'if the was an error writting the valueIf DisplayErrorMsg = True Then 'if the user want errors displayedMsgBox ErrorMsg(rtn) 'display the errorEnd IfEnd Ifrtn = RegCloseKey(hKey) 'close the keyElse 'if there was an error opening the keyIf DisplayErrorMsg = True Then 'if the user wants errors displayedMsgBox ErrorMsg(rtn) 'display the errorEnd IfEnd IfEnd IfEnd FunctionFunction GetBinaryValue(SubKey As String, Entry As String)Call ParseKey(SubKey, MainKeyHandle)If MainKeyHandle Thenrtn = RegCreateKey(MainKeyHandle, SubKey, hKey) 'open the keyIf rtn = ERROR_SUCCESS Then 'if the key could be opened lBufferSize = 1rtn = RegQueryValueEx(hKey, Entry, 0, REG_BINARY, 0, lBufferSize) 'get the value from the registrysBuffer = Space(lBufferSize)rtn = RegQueryValueEx(hKey, Entry, 0, REG_BINARY, sBuffer, lBufferSize) 'get the value from the registryIf rtn = ERROR_SUCCESS Then 'if the value could be retreived thenrtn = RegCloseKey(hKey) 'close the keyGetBinaryValue = sBuffer 'return the value to the userElse 'otherwise, if the value couldnt be retreivedGetBinaryValue = "Error" 'return Error to the userIf DisplayErrorMsg = True Then 'if the user wants to errors displayedMsgBox ErrorMsg(rtn) 'display the error to the userEnd IfEnd IfElse 'otherwise, if the key couldnt be openedGetBinaryValue = "Error" 'return Error to the userIf DisplayErrorMsg = True Then 'if the user wants to errors displayedMsgBox ErrorMsg(rtn) 'display the error to the userEnd IfEnd IfEnd IfEnd FunctionFunction DeleteKey(Keyname As String)Call ParseKey(Keyname, MainKeyHandle)If MainKeyHandle Thenrtn = RegCreateKey(MainKeyHandle, Keyname, hKey) 'open the keyIf rtn = ERROR_SUCCESS Then 'if the key could be opened thenrtn = RegDeleteKey(hKey, Keyname) 'delete the keyrtn = RegCloseKey(hKey) 'close the keyEnd IfEnd IfEnd FunctionFunction GetMainKeyHandle(MainKeyName As String) As LongConst HKEY_CLASSES_ROOT = &H80000000Const HKEY_CURRENT_USER = &H80000001Const HKEY_LOCAL_MACHINE = &H80000002Const HKEY_USERS = &H80000003Const HKEY_PERFORMANCE_DATA = &H80000004Const HKEY_CURRENT_CONFIG = &H80000005Const HKEY_DYN_DATA = &H80000006Select Case MainKeyNameCase "HKEY_CLASSES_ROOT"GetMainKeyHandle = HKEY_CLASSES_ROOTCase "HKEY_CURRENT_USER"GetMainKeyHandle = HKEY_CURRENT_USERCase "HKEY_LOCAL_MACHINE"GetMainKeyHandle = HKEY_LOCAL_MACHINECase "HKEY_USERS"GetMainKeyHandle = HKEY_USERSCase "HKEY_PERFORMANCE_DATA"GetMainKeyHandle = HKEY_PERFORMANCE_DATACase "HKEY_CURRENT_CONFIG"GetMainKeyHandle = HKEY_CURRENT_CONFIGCase "HKEY_DYN_DATA"GetMainKeyHandle = HKEY_DYN_DATAEnd SelectEnd FunctionFunction ErrorMsg(lErrorCode As Long) As String'If an error does accurr, and the user wants error messages displayed, then'display one of the following error messagesSelect Case lErrorCodeCase 1009, 1015GetErrorMsg = "The Registry Database is corrupt!"Case 2, 1010GetErrorMsg = "Bad Key Name"Case 1011GetErrorMsg = "Can't Open Key"Case 4, 1012GetErrorMsg = "Can't Read Key"Case 5GetErrorMsg = "Access to this key is denied"Case 1013GetErrorMsg = "Can't Write Key"Case 8, 14GetErrorMsg = "Out of memory"Case 87GetErrorMsg = "Invalid Parameter"Case 234GetErrorMsg = "There is more data than the buffer has been allocated to hold."Case ElseGetErrorMsg = "Undefined Error Code: " & Str$(lErrorCode) End SelectEnd FunctionFunction ClearKK(Str As String)Dim I As LongDim K As StringFor I = 1 To Len(Str)K = Mid(Str, I, 1)If K = Chr(0) ThenStr = Mid(Str, 1, I)Exit ForEnd IfNextEnd FunctionFunction GetStringValue(SubKey As String, Entry As String) Call ParseKey(SubKey, MainKeyHandle)If MainKeyHandle Thenrtn = RegCreateKey(MainKeyHandle, SubKey, hKey) 'open the keyIf rtn = ERROR_SUCCESS Then 'if the key could be opened thensBuffer = Space(255) 'make a bufferlBufferSize = LenB(StrConv(sBuffer, vbFromUnicode))rtn = RegQueryValueEx(hKey, Entry, 0, REG_SZ, sBuffer, lBufferSize) 'get the value from the registryIf rtn = ERROR_SUCCESS Then 'if the value could be retreived thenrtn = RegCloseKey(hKey) 'close the keysBuffer = Trim(sBuffer)ClearKK sBufferGetStringValue = Trim(Left(sBuffer, LenB(StrConv(sBuffer, vbFromUnicode)) - 1)) 'return the value to the userElse 'otherwise, if the value couldnt be retreivedGetStringValue = "Error" 'return Error to the userIf DisplayErrorMsg = True Then 'if the user wants errors displayed thenMsgBox ErrorMsg(rtn) 'tell the user what was wrongEnd IfEnd IfElse 'otherwise, if the key couldnt be openedGetStringValue = "Error" 'return Error to the userIf DisplayErrorMsg = True Then 'if the user wants errors displayed thenMsgBox ErrorMsg(rtn) 'tell the user what was wrongEnd IfEnd IfEnd IfEnd FunctionPrivate Sub ParseKey(Keyname As String, Keyhandle As Long)rtn = InStr(Keyname, "\") 'return if "\" is contained in the KeynameIf Left(Keyname, 5) <> "HKEY_" Or Right(Keyname, 1) = "\" Then 'if the is a "\" at the end of the Keyname thenMsgBox "Incorrect Format:" + Chr(10) + Chr(10) + Keyname 'display error to the userExit Sub 'exit the procedureElseIf rtn = 0 Then 'if the Keyname contains no "\"Keyhandle = GetMainKeyHandle(Keyname)Keyname = "" 'leave Keyname blankElse 'otherwise, Keyname contains "\"Keyhandle = GetMainKeyHandle(Left(Keyname, rtn - 1)) 'seperate the KeynameKeyname = Right(Keyname, LenB(StrConv(Keyname, vbFromUnicode)) - rtn)End IfEnd SubFunction CreateKey(SubKey As String)Call ParseKey(SubKey, MainKeyHandle)If MainKeyHandle Thenrtn = RegCreateKey(MainKeyHandle, SubKey, hKey) 'create the keyIf rtn = ERROR_SUCCESS Then 'if the key was created then rtn = RegCloseKey(hKey) 'close the keyEnd IfEnd IfEnd Function'第三部分,完结.Function SetStringValue(SubKey As String, Entry As String, Value As String)Call ParseKey(SubKey, MainKeyHandle)If MainKeyHandle Thenrtn = RegCreateKey(MainKeyHandle, SubKey, hKey) 'open the keyIf rtn = ERROR_SUCCESS Then 'if the key was open successfully thenrtn = RegSetValueEx(hKey, Entry, 0, REG_SZ, ByVal Value, LenB(StrConv(Value, vbFromUnicode))) 'write the value If Not rtn = ERROR_SUCCESS Then 'if there was an error writting the valueIf DisplayErrorMsg = True Then 'if the user wants errors displayedMsgBox ErrorMsg(rtn) 'display the errorEnd IfEnd Ifrtn = RegCloseKey(hKey) 'close the keyElse 'if there was an error opening the keyIf DisplayErrorMsg = True Then 'if the user wants errors displayedMsgBox ErrorMsg(rtn) 'display the errorEnd IfEnd IfEnd IfEnd FunctionFunction DelValue(SubKey As String, Entry As String)Call ParseKey(SubKey, MainKeyHandle)If MainKeyHandle Thenrtn = RegCreateKey(MainKeyHandle, SubKey, hKey) 'open the keyIf rtn = ERROR_SUCCESS Then 'if the key could be opened thenrtn = RegDeleteValue(hKey, Entry)End IfEnd IfEnd Function。

VB.NET获取计算机系统信息

获取计算机系统信息Visual 拥有许多VB开发者以前只能梦想的新功能和新特色。

比如说,在VB6中只有通过复杂的Win32 API函数调用才能获得的系统信息,到了,就可以以多种方法轻松的获取。

一、Environment类轻松获取系统信息.NET Framework中的System.Environment类提供了丰富而简单的方法取得有关当前环境和平台的系统信息,如计算机名称、系统目录等:获取计算机的 NetBIOS 名称:Environment类的MachineName属性可以轻松获取此本地计算机的 NetBIOS 名称,如:lblMachineName.T ext = "机器名:" & Environment.MachineName获取操作系统标识符和版本号:Environment类的OSVersion可以轻松获取当前操作系统标识符和版本号,如:lblOs.Text = "操作系统:" & Environment.OSVersion.ToString 获取系统目录:Environment类的SystemDirectory属性可以轻松取得系统目录:LblSysDir.Text = "系统目录:" & Environment.SystemDirectory获取系统启动后经过的时间:Environment类的TickCount属性可以轻松获取系统启动后经过的毫秒数,如:LblSysTickCount.Text = "系统已经启动了:" &CStr(Environment.TickCount / 1000) & "秒"获取系统特殊文件夹的路径:使用Environment类的GetFolderPath方法可以轻松获得系统特殊文件夹的路径,如:LblSpecialDir.Text = "我的收藏夹:" & _ mEnvironment.GetFolderPath(Environment.SpecialFolder.Favorit es)获取当前用户名:Environment类的UserName属性可以轻松获取当前用户名,如:LblUser.Text = "用户:" & erName二、SystemInformation类轻松获取系统信息通过SystemInformation类也可以轻松取得如计算机名等操作系统的有关信息:获取系统的启动方式:使用 BootMode 可确定用户如何启动系统。

VB利用GetObject(WinMgmts)获取系统信息

Set wbemObjectSet = wbemServices.InstancesOf("Win32_LogicalMemoryConfiguration")
For Each wbemObject In wbemObjectSet
WScript.Echo "物理内存 (MB): " & CInt(wbemObject.TotalPhysicalMemory/1024)
用WMI对象列出系统所有进程:
----Instance.vbs---- 程序代Βιβλιοθήκη Dim WMI,objs
Set WMI = GetObject("WinMgmts:")
Set objs = WMI.InstancesOf("Win32_Process")
For Each obj In objs
next
msgbox infor
CPU的序列号:
---CPUID.vbs---
程序代码
Dim cpuInfo
cpuInfo = ""
set moc = GetObject("Winmgmts:").InstancesOf("Win32_Processor")
for each mo in moc
for each mo in moc
HDid = mo.Model
msgbox "硬盘型号为:" & HDid
next
网卡MAC物理地址:
---MACAddress.vbs---
程序代码
Dim mc

VB中如何实现注册表的读写

---- 毫不夸张的说,注册表相当于WIN95的“心”,几乎所有的Windows应用程序都毫不例外的要在这里边“安家筑巢”,如果你也用VB编写了一个程序,是不是也想在注册表里占有一席之地呢(即使刚才没有,现在大概也有了吧)?嗯,听我慢慢道来。

---- “API”中专门提供了处理注册表的函数,不用说也知道,它们肯定功能强大(因为它们是API),但是它们的难度也是不小的。

如果你是一个入门者的话,这大概会令你头痛一阵子。

不过VB本身还提供了几个函数,专门用来处理注册表,相对来说它们就简单多了,所以我只对它们做一下说明:SaveSetting appname, section, key, value---- 这句话的作用是将应用程序的信息存入注册表中。

其中四个参数都是必要的,其含义分别为:应用程序名,区段,键,值。

比如我们要为我们的程序(名为“测试”)的主窗体的位置进行存储,就可以写成:SaveSetting “测试”,”main”,”top”,frmMain.TopSaveSetting “测试”,”main”,”Left”,frmMain.LeftSaveSetting “测试”,”main”,”Width”,frmMain.WidthSaveSetting “测试”,”main”,”Height”,frmMain.Height---- 这样,就把当前的主窗体的位置进行了记录,在下次打开这个窗体的时候,我们就可以在把这些值取出来。

那么如何才能把我们所存储的值取出来呢?这就要用到另一个函数:GetSetting,它的语法是:GetSetting(appname, section, key[, default])---- 该函数的各部分的意义均和上面的函数的意义相同,所不同的是多了一个default而少了一个value。

这个函数最后返回的结果即value值,而default指的是如果所指定的键不存在时,则以default指定的值为返回结果。

VB.NET文件读写操作

这一部分,你将找到更多常用的文件操作的代码实例。

最常用、最基本的操作就是把text写入文件和读回来。

现在的应用程序通常不用二进制文件作存储简单的变量,而用它来存储对象,对象集合以及其他机器代码。

下面,将看到具体操作的例子。

读写文本文件为了把text保存到文件,创建一个基于FileStream的StreamReader对象,然后调用Write方法把需要保存的text写入文件。

下面的代码用SaveFileDialog 提示用户指定一个文件,用于保存TextBox1的内容。

同样采用类似的语句,我们读取一个文本文件,并把内容显示在TextBox控件中。

StreamReader的ReadToEnd方法返回文件的全部内容。

各种对象的存储采用BinaryFormatte以二进制的形式,或者用SoapFormatter类以XML格式都可以序列化一个具体的对象。

只要把所有BinaryFormatter的引用改为SoapFormatter,无需改变任何代码,就可以以XML格式序列化对象。

首先创建一个BinaryFormatter实例:然后创建一个用于存储序列化对象的FileStream对象:接着调用BinFormatter的Serialize方法序列化任何可以序列化的framework 对象:加一个Serializable属性使得自定义的对象可以序列化下面代码创建一个Person对象实例,然后调用BinFormatter的Serialize方法序列化自定义对象:你也可以在同一个Stream中接着序列化其他对象,然后以同样的顺序读回。

例如,在序列化Person对象之后接着序列化一个Rectangle对象:创建一个BinaryFormatter对象,调用其Deserialize方法,然后把返回的值转化为正确的类型,就是整个反序列化过程。

然后接着发序列化Stream的其他对象。

假定已经序列化了Person和Rectangle两个对象,以同样的顺序,我们反序列化就可以得到原来的对象:大多数程序处理对象集合而不是单个的对象。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档