Tìm thủ thuật nhanh hơn với chức năng tìm trong Blog

03/06/2010

Thao tác với registry (nâng cao)

Trước đây mình có giới thiệu 1 bài viết giúp chúng ta thao tác với registry. Phạm vi bài viết đó chỉ là ghi/ đọc 1 giá trị vào đó nhằm tiện lợi cho lần mở chương trình kế tiếp. Các bạn có thể ứng dụng tùy biến cho user save password hay vài giá trị mà user muốn mặc định...

Xem thêm

Tuy nhiên, do một số nhu cầu đặc biệt, bạn muốn can thiệp sâu hơn vào registry. Điều này đòi hỏi bạn phải có chút am hiểu về nó. Ứng dụng là bạn có thể change workgroup, tạo ổ mạng, setting vài mặc định cho access hay các ứng dụng khác...


Dưới đây mình sẽ giới thiệu vài hàm hỗ trợ ta thao tác với registry :


Reading from the Registry:


Code:

'reads the value for the registry key i_RegKey
'if the key cannot be found, the return value is ""
Function RegKeyRead(i_RegKey As String) As String
Dim myWS As Object

On Error Resume Next
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'read key from registry
RegKeyRead = myWS.RegRead(i_RegKey)
End Function


Checking if a Registry key exists:

Code:

'returns True if the registry key i_RegKey was found
'and False if not
Function RegKeyExists(i_RegKey As String) As Boolean
Dim myWS As Object

On Error GoTo ErrorHandler
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'try to read the registry key
myWS.RegRead i_RegKey
'key was found
RegKeyExists = True
Exit Function

ErrorHandler:
'key was not found
RegKeyExists = False
End Function


Saving a Registry key:

Code:

'sets the registry key i_RegKey to the
'value i_Value with type i_Type
'if i_Type is omitted, the value will be saved as string
'if i_RegKey wasn't found, a new registry key will be created
Sub RegKeySave(i_RegKey As String, _
i_Value As String, _
Optional i_Type As String = "REG_SZ")
Dim myWS As Object

'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'write registry key
myWS.RegWrite i_RegKey, i_Value, i_Type

End Sub


Deleting a key from the Registry:

Code:

'deletes i_RegKey from the registry
'returns True if the deletion was successful,
'and False if not (the key couldn't be found)
Function RegKeyDelete(i_RegKey As String) As Boolean
Dim myWS As Object

On Error GoTo ErrorHandler
'access Windows scripting
Set myWS = CreateObject("WScript.Shell")
'delete registry key
myWS.RegDelete i_RegKey
'deletion was successful
RegKeyDelete = True
Exit Function

ErrorHandler:
'deletion wasn't successful
RegKeyDelete = False
End Function


Ví dụ sau cho phép bạn startip chương trình unikey lưu trong thư mục D:\\Soft\\UniKey4.0\\unikey40RC2-1101-win32\\UniKeyNT.exe


Key của nó muốn lưu trong registry là

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]


"'UniKey"="D:\\Soft\\UniKey4.0\\unikey40RC2-1101-win32\\UniKeyNT.exe"


Ta phát biểu:

Dim key as String, v as String

key="[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]

'UniKey'"

v="D:\\Soft\\UniKey4.0\\unikey40RC2-1101-win32\\UniKeyNT.exe"

RegKeySave key,v


Chúc thành công!


Ngoài ra việc thao tác với registry bạn phải thật cẩn thận vì có thể làm hỏng cả hệ thống. Nên trước khi làm việc, bạn nên sao lưu 1 bản dự phòng bất trắc.
____________________________________________________________________________________
Thảo luận thêm: http://thuthuataccess.com/forum

1 nhận xét:

  1. The runner system snaps off Inkjoy Gel Pens as the part is ejected from the mold, eliminating the need for guide removal. The gate (is the entry level of the material into the cavity of the mold. Its geometry and location is necessary, as it determines the circulate of the plastic. Burn marks sometimes seem as black or rust-colored discoloration on an edge or surface of a molded plastic part. Burn marks generally don’t result on} part integrity, unless the plastic is burned to the extent of degradation.

    Trả lờiXóa

» Hãy để lại tên, email của bạn khi bạn post comment, để mình có thể dễ dàng trả lời comment của bạn khi cần.
» Có thể sử dụng các thẻ < b>,< i>,< a>. Và các icon thông dụng trên YM