利用Windows注册表存储信息

王朝system·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

一、TRegIniFile方法:()

首先,在源文件(cpp)中加入

#include <vcl\registry.hpp> file://for C++ Builder 3.0

__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)

{ // 当Form1打开时,从注册表读入Left值

TRegIniFile *pIni = new TRegIniFile("SOFTWARE\\MyName\\MyProg");

Left = pIni->ReadInteger("Config","Left",200);

delete pIni;

}

void __fastcall TForm1::FormClose(TObject *Sender,TCloseAction &Action)

{ // 当Form1关闭时,往注册表存放Left值

TRegIniFile *pIni = new TRegIniFile("SOFTWARE\\MyName\\MyProg");

pIni->WriteInteger("Config","Left",Left);

delete pIni;

}

注:在缺省状态下,信息存放HKEY_CURRENT_USER根目录下。实际上,它代替了Windows 3.1中的Ini文件.若存放在其它根目录下,可设置Registry->RootKey,例:

Registry->RootKey = HKEY_LOCAL_MACHINE;

除了可以存放整型值,还可以存放字符串值和布尔值。

二、如何操作注册表

#include <Registry.hpp>

TRegIniFile *R=new TRegIniFile("SOFTWARE\\ahao's Softwares\\Wenku\\Config");

file://读

Edit2->Text=R->ReadString("SMTP","Host","");

Edit3->Text=IntToStr(R->ReadInteger("SMTP","Port",25));

Edit4->Text=R->ReadString("SMTP","UserID","");

file://写

R->WriteString("SMTP","Host",Edit2->Text);

R->WriteInteger("SMTP","Port",StrToInt(Edit3->Text));

R->WriteString("SMTP","UserID",Edit4->Text);

别忘了 R->Free();

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航