Use the program to implement the association

xiaoxiao2021-03-06  79

The file association has an important role in programming. Its purpose is to implement some extension files to open, there are many software, especially trials those media playing software, mostly implement the association of the file, You can imagine that a file associated with the file is not done, how does it mix? In addition, since the general application has its own specific extension data file, this also requires software compilation to implement automatic associations in the program without being manually implemented by the user. How to achieve the association of the file? The core of implementing file association is the operation of the registry, all file associations are under HKEY-CLASSES - ROOT, to implement file associations to add two key values ​​in HKEY-CLASSES-ROOT, one is corresponding to the file extension Type Description, the second is the application that needs to be performed on this type of file. The following example can explain how to implement the association of the file. The function of this example is to associate the file that the user specifies the extension name with the user specified by the user, and double-click the type of file to open. The sample program is implemented with Borland C Builder, and part of the code is as follows:

Connect to the document:

Void __fastcall tform1 :: btnassoclick (TOBJECT * Sender)

{

Tregistry * reg = new tregistry;

REG-> rootkey = hkey_classes_root;

Try

{

REG-> OpenKey (EXTDOWIT-> Text), true);

REG-> Writestring ("", appdowith (edtapppath-> text);

REG-> CloseKey ();

REG-> OpenKey (AppDowith (edtApppath-> text) "// shell // open // command", true);

REG-> WRITESTRING ("", edtapppath-> text);

REG-> CloseKey ();

}

__finally

{

DELETE REG;

}

}

The above code implements the association of the file, which uses C Builder to provide the Tregistry component, which provides many ways to operate the registry, such as: OpenKey opens a key to the registry, WriteString to the registration table, write value, etc. This avoids using Win32API, such as REGCREATEKEY, RegSetValue, etc. to complete the same function. EXTDOWITH (), appdowith () The functionality of custom functions is the extension of the associated file and the file name of the application, the code is as follows:

EXTDOWITH () function:

ANSISTRING __FASTCALL TFORM1 :: EXTDOWITH (ANSISTRING STR)

{

// Todo: Add your source code here

// Put forward in the extension. Symbol

IF (Str.Substring (0, 1)! = ".")

{

Str = "." STR;

}

Return Str;

}

Appdowith () function:

ANSISTRING __FASTCALL TFORM1 :: Appdowith (Ansistring AppPath)

{

// Todo: Add your source code here

/ / Get the file name of the application, without extension

INT length = apppath.length ();

INDEX = AppPath.lastdelimiter ("//"); apppath = apppath.substring (INDEX 1, Length-INDEX 1);

Index = apppath.lastdelimiter (".");

AppPath = AppPath.Substring (0, INDEX-1);

Return apppath;

}

This enables the association of file types and applications. If you are interested in this program, you can download the project file from http://www.zcfamily.com/zqget/ URL. If you have an idea with me, my contact information: email: zqget@msn.com

转载请注明原文地址:https://www.9cbs.com/read-120752.html

New Post(0)