Delphi Implementation file Association

Source: Internet
Author: User

File association brings us a lot of convenience. Delphi comes with a registry object Tregistry, which can be used to obtain or alter the contents of registry key values.

Function getassociatedexec (fileext:string; var filedescription, mimetype:string): String;

Var Reg:tregistry; filetype:string;

Begin

Result: =′′; {The function return value is the name of the executable program that opened the Fileext file and its arguments}

Reg: = tregistry.create; {Create an instance of a registry object}

Try

Reg.rootkey: = Hkey-classes-root; {Registry root key for prepare operation}

If not Reg.openkey (Fileext, False) then Exit; {Exits when an incoming file type does not exist in the registry, False indicates that a new key is not created automatically when the Fileext key value does not exist}

FileType: = reg.readstring (′′); {A simple description of the file type}

MIMEType: = reg.readstring (′content type′); {Content MIME type}

Reg.closekey; {Close current keyword}

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

FileDescription: = reg.readstring (′′); {Specific description of the content type}

If not Reg.openkey (′shell\open\command′, False) then Exit; {The key value is saved by which program, with what parameters open the file of type Fileext}

Result: = reg.readstring (′′); {Read the contents of this key}

Reg.closekey; Finally

Reg.free; {Dispose of Object instances}

End End

By the above routines, it is first found in hkey-classes-root with fileext (file suffix, with ".") ) to match the primary key, and then obtain the key value from the "Default" key name, and then take the key value as the primary key to get a specific description of the Fileext. In its subkey Shell\open\command, you can control which program opens a file of type Fileext, as long as you modify its contents, and you will be able to manage which programs open files of type Fileext.

Now that you know how file associations are defined in the registry, you can correctly change the association of files as long as they are in the opposite way.

function Setassociatedexec (Fileext, Filetype, filedescription,mimetype,execname:string): Boolean; {The modification succeeds, returns TRUE, otherwise false}

var reg:tregistry;

Begin

Result: = False; {}

if (Fileext =′′) or (Execname =′′) then Exit; {If the file type is empty or does not have a program defined to exit, Fileext must take ″.″, such as. BMP}

Reg: = tregistry.create;

Try

Reg.rootkey: = Hkey-classes-root;

If not Reg.openkey (Fileext, True) then Exit; {Exit When the Fileext key is not found or created correctly, this is usually a registry error, the same as}

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); {The execution program generally has parameters, such as WinZip ' Winzip32.exe″%1″ ', and the ″%1″ parameter refers to the file name of the zip file. So execname should add parameters as appropriate}

Reg.closekey;

Finally Reg.free; End

End

Writing and reading is a process of the same nature and opposite direction, so there is not too much explanation in the Setassociatedexec function, you can refer to the Getassociatedexec function. As for the use of tregistry, confined to space, this article withheld.

Delphi Implementation file Association

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.