在delphi中輕鬆操作登錄檔
tregistry類
在應用程式中,經常需要對登錄檔進行各種操作,如將程式名稱、安裝路徑等資訊儲存到登錄檔中。delphi對相應api函式做了進一步封裝,使得在應用程式中操作登錄檔更加簡單。
1、原理
delphi提供了tregistry類,它直接繼承自tobject類,主要屬性有:rootkey等,主要方法有:
create,
createkey(key),
openkey(key,cancreate),
writestring(name,value),readstring(name),
closekey,
free等,下面對它們進行簡單介紹。
1、1、rootkey屬性
指定當前操作的登錄檔主鍵,共有hkey_classes_root、hkey_current_user、hkey_local_machine、hkey_users、hkey_current_config五種取值,預設值為hkey_current_user。
1、2、createkey方法
函式宣告為function createkey(const key: string):boolean;,用於在登錄檔中新建項,項的名稱為引數key的值。
1、3、openkey方法
函式宣告為function openkey(const key: string; cancreate: boolean):boolean;,其中引數key指定要開啟的登錄檔的項,引數cancreate指明當要開啟的項不存在時是否建立,
預設值為false;函式返回值為布林型別,如果開啟成功返回true,否則返回false。
1、4、writestring方法
過程宣告為procedure writestring(const name, value: string);,其中引數name指明要寫入的登錄檔的字串的名稱,引數value為要寫入的字串的值。如果該字串不存在則寫入
,否則覆蓋原來的取值。
1、5、readstring方法
函式宣告為function readstring(const name: string):string;,其中引數name為要讀取的字串的名稱,函式返回值為要讀取的字串的值,是字串型別。如果該字串在登錄檔中不存在,返回值為空字串
。2、例項
下面分別為將資訊寫入登錄檔和從登錄檔中讀取資訊的例子。
2、1、將資訊寫入登錄檔
相應**如下:
varreg:tregistry;//宣告乙個tregistry類變數
...begin
reg:=tregistry.create;//建立例項
reg.rootkey:=hkey_current_user;//指定需要操作的登錄檔的主鍵
if reg.openkey('/software/delphi使用技巧',true) then//如果開啟成功則進行以下操作
begin
reg.writestring('文章名稱','在delphi中輕鬆操作登錄檔');//將需要儲存的資訊寫入登錄檔
reg.closekey;//關閉登錄檔
end;
reg.free;//釋放變數所佔記憶體
...end;
2、2、從登錄檔中讀取資訊
相應**如下:
varreg:tregistry;//宣告乙個tregistry類變數
s:string;//宣告乙個字串變數用來存放要讀取的資訊
...begin
reg:=tregistry.create;//建立例項
reg.rootkey:=hkey_current_user;//指定需要操作的登錄檔的主鍵
if reg.openkey('/software/delphi使用技巧',true) then//如果開啟成功則進行以下操作
begin
s:=reg.readstring('文章名稱');//從登錄檔中讀取對應字串的值
//如果該字串不存在則返回值為空字串
reg.closekey;//關閉登錄檔
end;
reg.free;//釋放變數所佔記憶體
...end;
3、總結
本文首先介紹了在delphi中操作登錄檔的方法,然後舉例說明了具體應用。
在C 中輕鬆操作登錄檔
visual studio net以前的版本,要對登錄檔進行修改,則需要呼叫系統api,而現在則不用那麼麻煩,因為.net已經把登錄檔相關的操作封裝到乙個類中,呼叫的時候只要只要呼叫此類物件相應的屬性或方法即可。以下就登錄檔這個類進行說明。首先,要引入註冊類所在的nampespace,如下 接下來就...
在Delphi程式中操作登錄檔
32位delphi程式中可利用tregistry物件來訪問登錄檔檔案中的資訊。一 建立和釋放tregistry物件 1.建立tregistry物件。為了操作登錄檔,要建立乙個tregistry物件 aregistry tregistry.create 2.釋放tregistry物件。對登錄檔操作結束...
Delphi中讀寫登錄檔
delphi程式中入如何操作登錄檔,在應用程式中,經常需要對登錄檔進行各種操作,如將程式名稱 安裝路徑等資訊儲存到登錄檔中。32位delphi程式中可利用tregistry物件來訪問登錄檔檔案中的資訊。一 建立和釋放tregistry物件 1.建立tregistry物件。為了操作登錄檔,要建立乙個t...