怎么用TRegistry?

liwenqiu_2001 2002-04-18 02:24:24
我想输出注册表里某一键值下所有的数值的名称和数值数据,因该怎么做呢?
...全文
433 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
CCED136 2002-04-19
  • 打赏
  • 举报
回复
再来讨论讨论:
假如要取得
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion
下的所有子键名称,则应该使用:
Registry->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",false);
Registry->GetKeyNames(ListBox1->Items);

在下面的循环中,应该在读取数据以前,先判断将要读取数据的类型。先看
for (int i=0;i<ListBox1->Items->Count; i++)
{
s=Registry->ReadString(ListBox1->Items->Strings[i]);
Memo1->Lines->Add("名称:"+ListBox1->Items->Strings[i]+" 值:"+s);
}
在这里,进入循环后,首先应该使用 GetDataType(ListBox1->Items->String[i]) 判断数据类型,然后才能确定使用哪一种方法读取数据。(ReadString ,ReadInteger还是其他的方法)


CCED136 2002-04-19
  • 打赏
  • 举报
回复
好了,现在写了一个简单的:

//UNIT1.H
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <Registry.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TListBox *ListBox1;
TButton *Button1;
TButton *Button2;
TMemo *Memo1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


//UNIT1.CPP
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
TRegistry *Registry = new TRegistry;
try
{
ListBox1->Items->Clear();
Registry->RootKey =HKEY_CURRENT_USER;
Registry->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",false);
Registry->GetValueNames(ListBox1->Items);
Registry->CloseKey();
}
__finally
{
delete Registry;
ListBox1->ItemIndex=0;
}
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
TRegistry *Registry = new TRegistry;
AnsiString s;

if ( ListBox1->Items->Count )
{
Registry->RootKey =HKEY_CURRENT_USER;
Registry->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",false);
for (int i=0;i<ListBox1->Items->Count; i++)
{
s=Registry->ReadString(ListBox1->Items->Strings[i]);
Memo1->Lines->Add("键:"+ListBox1->Items->Strings[i]+" 值:"+s);
}
Registry->CloseKey();
}
delete Registry;
}
//---------------------------------------------------------------------------


CCED136 2002-04-19
  • 打赏
  • 举报
回复
现在我的手上没有关于 run 键值下面的获取代码,让我找一找,要不然给你写一个!
liwenqiu_2001 2002-04-19
  • 打赏
  • 举报
回复
CCED136() 仁兄,可否给个例子看看,比如我想知道Run下所以自动运行的程序的名字和路径,但我并不知道有什么程序!
Chimae 2002-04-18
  • 打赏
  • 举报
回复
#include <Registry.hpp>
TRegistry *theRegistry;
theRegistry = new TRegistry;
theRegistry->RootKey = HKEY_LOCAL_MACHINE;
AnsiString S;
try{
theRegistry->OpenKey("SOFTWARE\\自己取", true);
Name = theRegistry->ReadString("自己取");
//ReadString,WriteString...
theRegistry->CloseKey();
}
catch(Exception &E){
Name = "";
}
delete theRegistry;
CCED136 2002-04-18
  • 打赏
  • 举报
回复
在给你一点东东:

void __fastcall GetKeyNames( class::TStrings *String );

所有的子键名称全部存储在 String 列表里面;

void __fastcall GetValueNames( class::TStrings *String );

所有的关联数值名称存储在 String 列表里面。

剩下的,因该没问题了吧 :)



CCED136 2002-04-18
  • 打赏
  • 举报
回复
注意了:

在 TRegistry 中,使用 GetKeyNames 方法,可以取得当前键下面的

所有的子键名称。

使用 GetValueNames方法,取得当前键关联的所有数值名称。
常宁 2002-04-18
  • 打赏
  • 举报
回复
If you are writing a Windows-only application and are comfortable with the structure of the system Registry, you can use TRegistry. Unlike TRegistryIniFile, which uses the same properties and methods of other ini file components, the properties and methods of TRegistry correspond more directly to the structure of the system Registry. For example, TRegistry lets you specify both the root key and subkey, while TRegistry assumes HKEY_CURRENT_USER as a root key. In addition to methods for opening, closing, saving, moving, copying, and deleting keys, TRegistry lets you specify the access level you want to use.

Note: TRegistry is not available for cross-platform programming.

The following example retrieves a value from a registry entry:

#include <Registry.hpp>

AnsiString GetRegistryValue(AnsiString KeyName)
{
AnsiString S;
TRegistry *Registry = new TRegistry(KEY_READ);
try
{
Registry->RootKey = HKEY_LOCAL_MACHINE;
// False because we do not want to create it if it doesn't exist
Registry->OpenKey(KeyName,false);
S = Registry->ReadString("VALUE1");
}
__finally
{
delete Registry;
}
return S;
}
注意这个东西进能用在windows平台,对于linux平台就不能用了,它不是CLX的一部分,而是VCL的
sczyq 2002-04-18
  • 打赏
  • 举报
回复
其中的 Registry 改为 reg
sczyq 2002-04-18
  • 打赏
  • 举报
回复
// .h 加入
#include <Registry.hpp>

// .cpp
TRegistry * reg = new TRegistry;

Registry->RootKey = HKEY_LOCAL_MACHINE;

if (reg->OpenKey("Software\\MySoft", false))
{
if (reg->ValueExists("Main windows"))
{
Edit1->Text = reg->ReadString("Main windows");
}
Registry->CloseKey();
}
delete Registry;

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧