利用Windows注册表存储信息 (转)

利用Windows注册表存储信息 (转)[@more@]

一、TRegIniFile方法:()
  首先,在源文件(cpp)中加入
  #include file://for C++ Builder 3.0

  __fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
  {  // 当Form1打开时,从注册表读入Left值
  TRegIniFile *pIni = new TRegIniFile("SOFTWAREMyNameMyProg");
  Left = pIni->ReadInteger("Config","Left",200);
  delete pIni;
  }

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

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

  TRegIniFile *pIni = new TRegIniFile("SOFTWAREMyNameMyProg");

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

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

  Registry->RootKey = HKEY_LOCAL_MACHINE;

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

二、如何操作注册表

#include

TRegIniFile *R=new TRegIniFile("SOFTWAREahao's SoftwaresWenkuConfig");

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


请使用浏览器的分享功能分享到微信等