当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++ Builder教程

利用Windows注册表存储信息

一、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();

相关内容
赞助商链接