用户登录  |  用户注册
首 页商业源码原创产品编程论坛
当前位置:PB创新网文章中心编程技巧编程其他

利用Windows注册表存储信息

减小字体 增大字体 作者:佚名  来源:本站整理  发布时间:2009-03-16 19:39:47

一、TRegIniFile方法:()
    首先,在源文件(cpp)中加入
            #include <vclegistry.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();


Tags:

作者:佚名

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
PB创新网ourmis.com】Copyright © 2000-2009 . All Rights Reserved .
页面执行时间:27,343.75000 毫秒
Email:ourmis@126.com QQ:2322888 蜀ICP备05006790号