
uses Registry;
function RegisterFileTypeCommand(fileExtension, menuItemText, target: string) : boolean;
var
reg: TRegistry;
fileType: string;
begin
result := false;
reg := TRegistry.Create;
with reg do
try
RootKey := HKEY_CLASSES_ROOT;
if OpenKey('.' + fileExtension, True) then
begin
fileType := ReadString('') ;
if fileType = '' then
begin
fileType := fileExtension + 'file';
WriteString('', fileType) ;
end;
CloseKey;
if OpenKey(fileType + '/shell/' + menuItemText + '/command', True) then
begin
WriteString('', target + ' "%1"') ;
CloseKey;
result := true;
end;
end;
finally
Free;
end;
end;
function UnRegisterFileTypeCommand(fileExtension, menuItemText: string) : boolean;
var
reg: TRegistry;
fileType: string;
begin
result := false;
reg := TRegistry.Create;
with reg do
try
RootKey := HKEY_CLASSES_ROOT;
if OpenKey('.' + fileExtension, True) then
begin
fileType := ReadString('') ;
CloseKey;
end;
if OpenKey(fileType + '/shell', True) then
begin
DeleteKey(menuItemText) ;
CloseKey;
result := true;
end;
finally
Free;
end;
end;
本文介绍了一种在Windows注册表中为特定文件扩展名注册和注销自定义命令的方法。通过Delphi代码实现,该方法可以创建关联菜单项并指定打开文件时运行的目标程序路径。
7629

被折叠的 条评论
为什么被折叠?



