怎样把一个文本框的值写入注册表,我的代码有错,请指教代码如下,(20分)

  • 主题发起人 主题发起人 wecluo
  • 开始时间 开始时间
W

wecluo

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button4Click(Sender: TObject);
var reg:tregistry;
begin
reg:=tregistry.create;
reg.RootKey:=hkey_local_machine;
if reg.OpenKey('/software/odbc/odbc.ini/odbc data sources',true) then
reg.writestring(edit2.text,'microsoft access driver (*.mdb)');
if reg.OpenKey('/software/odbc/odbc.ini',true) then
reg.CreateKey('''+edit2.text+''');
if reg.OpenKey('/software/odbc/odbc.ini/edit2.text',true) then
begin
reg.createkey('engines');
reg.WriteString('dbq',edit1.text);
reg.WriteString('driver',edit3.text);
reg.Writeinteger('driverid',25);
reg.WriteString('fil','MS Access;');
reg.Writeinteger('safetransactions',0);
reg.WriteString('uid','');
end;
if reg.openkey('/software/odbc/odbc.ini/edit2.text/engines',true) then
reg.createkey('jet');
if reg.OpenKey('/software/odbc/odbc.ini/edit2.Text/engines/jet',true) then
begin
reg.WriteString('implicitcommitsync','');
reg.writeinteger('maxbuffersize',512);
reg.Writeinteger('pagetimeout',10);
reg.Writeinteger('threads',3);
reg.WriteString('usercommitsync','yes');
end;

reg.Free;
showmessage('创建成功');
edit1.Text:='';
end;
 
你单步调试一下,看下那一句出错了。
 
调试过了,老实不能把文本框 的值 取出来
 
procedure TForm1.Button4Click(Sender: TObject);
var reg:tregistry;
begin
reg:=tregistry.create;
reg.RootKey:=hkey_local_machine;
if reg.OpenKey('/software/odbc/odbc.ini/odbc data sources',true) then
reg.writestring(edit2.text,'microsoft access driver (*.mdb)');
if reg.OpenKey('/software/odbc/odbc.ini',true) then
[red]reg.CreateKey('''+edit2.text+''');[/red] //只要 reg.CreateKey(edit2.text);就行了
if reg.OpenKey('/software/odbc/odbc.ini/edit2.text',true) then
begin
reg.createkey('engines');
reg.WriteString('dbq',edit1.text);
reg.WriteString('driver',edit3.text);
reg.Writeinteger('driverid',25);
reg.WriteString('fil','MS Access;');
reg.Writeinteger('safetransactions',0);
reg.WriteString('uid','');
end;
[red]if reg.openkey('/software/odbc/odbc.ini/edit2.text/engines',true) then[/red]
//reg.openkey('/software/odbc/odbc.ini/'+edit2.text+'/engines',false)
//下同
reg.createkey('jet');
[red]if reg.OpenKey('/software/odbc/odbc.ini/edit2.Text/engines/jet',true) then[/red]
begin
reg.WriteString('implicitcommitsync','');
reg.writeinteger('maxbuffersize',512);
reg.Writeinteger('pagetimeout',10);
reg.Writeinteger('threads',3);
reg.WriteString('usercommitsync','yes');
end;

reg.Free;
showmessage('创建成功');
edit1.Text:='';
end;
 
因該沒錯,不過你的Use中有沒有加入Registry類呢?
再還有就是你如果用的是WIndows2000的話,需要特別考慮用戶是否有權更改注冊表
 
procedure TForm1.Button4Click(Sender: TObject);
var reg:tregistry;
begin
reg:=tregistry.create;
reg.RootKey:=hkey_local_machine;
if reg.OpenKey('/software/odbc/odbc.ini/odbc data sources',true) then
reg.writestring(edit2.text,'microsoft access driver (*.mdb)');
if reg.OpenKey('/software/odbc/odbc.ini',true) then
[red]reg.CreateKey('''+edit2.text+''');[/red] //只要 reg.CreateKey(edit2.text);就行了
if reg.OpenKey('/software/odbc/odbc.ini/edit2.text',true) then (这行追加的是edit2.text而不是edit2.text 的值
begin
reg.createkey('engines');
reg.WriteString('dbq',edit1.text);
reg.WriteString('driver',edit3.text);
reg.Writeinteger('driverid',25);
reg.WriteString('fil','MS Access;');
reg.Writeinteger('safetransactions',0);
reg.WriteString('uid','');
end;
[red]if reg.openkey('/software/odbc/odbc.ini/edit2.text/engines',true) then[/red]
//reg.openkey('/software/odbc/odbc.ini/'+edit2.text+'/engines',false)
//下同
reg.createkey('jet');
[red]if reg.OpenKey('/software/odbc/odbc.ini/edit2.Text/engines/jet',true) then[/red]
begin
reg.WriteString('implicitcommitsync','');
reg.writeinteger('maxbuffersize',512);
reg.Writeinteger('pagetimeout',10);
reg.Writeinteger('threads',3);
reg.WriteString('usercommitsync','yes');
end;

reg.Free;
showmessage('创建成功');
edit1.Text:='';
end;
 
你先把edit.text赋给一个字符串变量,再将这个字符串变量和
前边的/software/odbc/odbc.ini/组成一个字符串变量,再用reg.OpenKey
对最后这个字符串openkey
如:
str:=edit1.text;
str:='/software/odbc/odbc.ini/' + str;
reg.OpenKey(str,true)
 
后退
顶部