文件关联
我想把我保存的文件与应用程序关联起来,就跟记事本那种程序一样,操作注册表吗?具体怎么做呢?
[解决办法]
直接上代码
bool __stdcall WriteStringToReg(HKEY Root, AnsiString Key, AnsiString KeyName, AnsiString KeyValue)
{
bool OK = false;
TRegistry *Registry = new TRegistry();
Registry-> RootKey = Root;
if(Registry-> OpenKey(Key, true))
{
try
{
if(Registry->ReadString(KeyName) != KeyValue)
{
Registry->WriteString(KeyName, KeyValue);
}
OK = true;
}
catch(...)
{
}
}
delete Registry;
return OK;
}
bool __stdcall RegFileType(AnsiString Ext, // 扩展名,如: ".dyc "
AnsiString ExtTypeName, // 文件类型,如: "dyc file "
AnsiString AppFileName, // 应用程序名,如 "c:\\dyc.exe "
AnsiString FileDescription, // 文件类型描述,如: "dyc documents "
int IconIndex) // 图标索引,如0
{
bool RegOK ;
String icon ;
RegOK = WriteStringToReg(HKEY_CLASSES_ROOT , "\"+Ext, "", ExtTypeName);
if(AppFileName.Pos( " ")> 0)
{
AppFileName = "" "+AppFileName+ "" ";
}
if(RegOK)
{
RegOK = WriteStringToReg(HKEY_CLASSES_ROOT, "\"+ExtTypeName, "", FileDescription);
}
if(RegOK)
{
icon = ChangeFileExt(ExtractFilePath(AppFileName),"player.ico") ;
//icon = AnsiString().sprintf(""%s", 1",AppFileName);
RegOK = WriteStringToReg(HKEY_CLASSES_ROOT, "\"+ExtTypeName+ "\\DefaultIcon", "", icon );
//RegOK = WriteStringToReg(HKEY_CLASSES_ROOT, "\"+ExtTypeName+ "\\DefaultIcon", "", "E:\\hyj\\bcb\\test\\colors.ico" );
}
if(RegOK)
{
RegOK = WriteStringToReg(HKEY_CLASSES_ROOT, "\"+ExtTypeName+ "\\shell\\open\\command", "", AppFileName+ " "%1" ");
}
return RegOK;
}