利用Windows登錄檔儲存資訊 (轉)

worldblog發表於2007-12-29
利用Windows登錄檔儲存資訊 (轉)[@more@]

一、TRegIniFile方法:()
  首先,在源(cpp)中加入
  #include 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(T *Sender,TCloseAction &Action)

  {  // 當Form1關閉時,往登錄檔存放Left值

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

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

注:在預設狀態下,資訊存放HKEY_CURRENT_USER根目錄下。實際上,它代替了 3.1中的Ini檔案.若存放在其它根目錄下,可設定Registry->Key,例:

  Registry->RootKey = HKEY_LOCAL_MACHINE;

除了可以存放整型值,還可以存放字串值和布林值。

二、如何操作登錄檔

#include

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


Edit2->Text=R->ReadString("SMTP","Host","");
Edit3->Text=IntToStr(R->ReadInteger("SMTP","Port",25));
Edit4->Text=R->ReadString("SMTP","UserID","");


R->WriteString("SMTP","Host",Edit2->Text);
R->WriteInteger("SMTP","Port",StrToInt(Edit3->Text));
R->WriteString("SMTP","UserID",Edit4->Text);

別忘了 R->Free();


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10752043/viewspace-996152/,如需轉載,請註明出處,否則將追究法律責任。

相關文章