Implementation file associated with Delphi

File Association brought us a lot of convenience. Delphi comes with registry objects TRegistry, you can get through it or change the contents of the relevant registry keys.

  Function GetAssociatedExec(FileExt: String; var FileDescription, MIMEType: String): String;

  Was Reg: TRegistry; File Type: String;

  begin

   Result: = ''; {FileExt open function returns the file is an executable program name and its parameters}

   Reg: = TRegistry.Create; {} create an instance of the registry object

   try

    Reg.RootKey: = HKey-Classes-Root; {preparation operation key registry root}

    if not Reg.OpenKey (FileExt, False) then Exit; {exit when an incoming file type does not exist in the registry, False said they did not automatically create a new key when FileExt key does not exist}

    FileType: = Reg.ReadString ( ''); {} brief description of the file type

    MIMEType: = Reg.ReadString ( 'Content Type'); {} MIME type content

    Reg.CloseKey; {close the current keyword}

    if (FileType = ′′) or (not Reg.OpenKey(FileType, False)) then Exit;

    FileDescription: = Reg.ReadString ( ''); {} specific description of the type

    if not Reg.OpenKey ( 'shell \ open \ command', False) then Exit; {which the key held by the program, as to what type of file parameters FileExt open}

    Result: = Reg.ReadString ( ''); {} reads the content key

    Reg.CloseKey; finally

    Reg.Free; {} release object instance

   end; end;

  Seen from the above routine, first in HKey-Classes-Root Key and looking FileExt (file extension, the ".") Matches, then takes the key from the "Default" button name, and then the key as a primary key to obtain FileExt detailed description. Save by which program to open the parameters of what type of information the file FileExt, modify its content as long as you can control the types of files open FileExt by the program in which their child key shell \ open \ command in.

  Now that you know the file association in the registry is how to define it, as long as doing exactly the opposite, you can change the file association of the right.

  function SetAssociatedExec (FileExt, Filetype, FileDescription, MIMEType, ExecName: String): Boolean; {modify successful, returns True, otherwise False}

  was Reg: TRegistry;

  begin

   Result := False;{}

   if (FileExt = '') or (ExecName = '') then Exit; { "." If the file type is not defined or is empty execution program exits, must take FileExt, such as .BMP}

   Reg := TRegistry.Create;

   try

  Reg.RootKey := HKey—Classes—Root;

  if not Reg.OpenKey (FileExt, True) then Exit; {when you can not find the right key to exit or create FileExt, this is usually the registry errors, following the same}

  Reg.WriteString(′′, FileType);

  if MIMEType <> ′′ then Reg.WriteString(′Content Type′, MIMEType);

  Reg.CloseKey;

  if not Reg.OpenKey(FileType, True) then Exit;

  Reg.WriteString(′′, FileDescription);

  if not Reg.OpenKey(′shell\open\command′, True) then Exit;

  Reg.WriteString ( '', ExecName); {executive typically have parameters, such as WinZip the "winzip32.exe"% 1 "", "% 1" parameter refers to the file name of the ZIP file. Thus ExecName be optionally added parameter}

  Reg.CloseKey;

   finally Reg.Free; end;

  end;

  A write and read the same nature, the process in the opposite direction, so there is no excessive described SetAssociatedExec function, reference may GetAssociatedExec function. As for the use TRegistry of the limited space, this article omitted.

Guess you like

Origin www.cnblogs.com/blogpro/p/11446120.html