xregistry.pas

来自「关于c++ builder编程的很好的资料」· PAS 代码 · 共 72 行

PAS
72
字号
unit xRegistry;

interface

uses Windows, SysUtils, Registry;

function GetAssociatedExecutable(FileExt: string; var FileDesc, MIMEType: string; sCommand: string = 'open'): string;
function SetAssociatedExecutable(FileExt, Filetype, FileDesc, MIMEType, ExecutablePath: string; sCommand: string = 'open'): Boolean;

function ReadKeyDefaultValue(REG: TRegistry; const sKey: string): string;
procedure WriteKeyDefaultValue(REG: TRegistry; const sKey, sValue: string);

implementation

uses xStrings;

function GetAssociatedExecutable(FileExt: string; var FileDesc, MIMEType: string; sCommand: string): string;
var
  Filetype: string; 
begin
  Result := '';
  
  if FileExt = '' then Exit;
  
  if FileExt[1] <> '.' then Filetype := FileExt;
  
  with TRegistry.Create do
    try
      RootKey := HKEY_CLASSES_ROOT;
      if Filetype = '' then
      begin
        if OpenKey(FileExt, False) then
        begin
          Filetype := ReadString('');
          MIMEType := ReadString('Content Type');
          CloseKey;
        end;
      end;
      
      if Filetype = '' then Exit;
      
      if OpenKey(Filetype, False) then
      begin
        FileDesc := ReadString('');
        
        if OpenKey('shell\' + sCommand + '\command', False) then
        begin
          Result := Trim(ReadString(''));
          
          // Truncate the paramters '%1' token from executable path
          // Ex. 'c:\winword.exe %1' -> 'c:\winword.exe'
          //          result := '"' + result + '"';
          if Length(Result) > 1 then
          begin
            Result := Trim(ExpandEnvStr(TrimParameters(unquotestring(Result))));
          end;
        end;
        CloseKey;
      end;
    finally
      Free;
    end;
end;

// FileType, FileDesc and MIMEType is optional.
function SetAssociatedExecutable(FileExt, Filetype, FileDesc, MIMEType, ExecutablePath: string; sCommand: string): Boolean;
begin
  Result := False;
  
  if (FileExt = '') or (ExecutablePath = '') then Exit;
  
  // 絋玂 FileExt 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?