How to implement a file type specified in the program

zhaozj2021-02-08  226

Home: http://www.maxssemail: maxss.net@163.com

During your daily use of your computer, we can open the associated software directly to open the file (such as a Word document) without having to use the software to open the file (such as a Word document) without the need to use the software to open this file. Such files. This is not only convenient for users, but also adds the initiative and flexibility of the software. How is this function of the software? Next I will introduce you how to increase such functions for software developed in the Windows environment. Friends who "play" through the Windows registry should find that there are many children's hkey_classes_root primary keys (such as .txt) beginning (such as .txt), and these child is the most critical start. Below I will explain the text file (* .txt) as an example. Open the menu "Start -> Run ...", enter "regedit" to start the Registry Editor. Then open the hkey_classes_root master key to find ".txt" child, click on the right to view the contents of the item on the right. Here we are mainly concerned about "(default)", because its content is the next primary key we have to find. In general, the key value is "txtfile", followed by finding the "txtfile" child in the hkey_classes_root primary key, and multiple children will appear after opening the item, and the purpose of the main child will be described below:

TXTFILE

- Defaulton (displayed file icon)

- shell

- Open

- Command (used to open the program path of this type of file)

- Print

- Command (used to print the program path of this type file)

With this information, as long as we modify or establish these related registry keys and key values ​​in the software, we can achieve the association of various file types.

Here is two functions I have written:

Bool

CheckFileRerance (ANSISTRING, ANSISTRING)

Void

RegisterFileRerance (Ansistring, Ansistring, Ansistring, Ansistring, Ansistring)

It is used to implement the function of detecting and modifying (new) files, and the source code is as follows:

/ / -------------------------------------------------------------------------------------------- ---------------------------

// Detect file association

// Exename: The extension to be detected (for example: ".txt")

// appkey: Exename extension key value in the registry (for example: "txtfile")

// Return true: indicates that it has been associated, false: indicates that it is not associated

Bool __fastcall checkfilerelation (ANSISTRING EXTNAME, ANSISTRING APPKEY)

{

Tregistry * preg = new tregistry ();

preg-> rootkey = hkey_classes_root; // Open the primary key

// When detecting that this program is not the default file association, please confirm the re-association operation.

preg-> OpenKey (EXTNAME, TRUE);

IF (preg-> readstring (")! = appkey)

{

Preg-> CloseKey ();

Return False;

}

Preg-> CloseKey ();

Return True;

}

/ / -------------------------------------------------------------------------------------------- -------------------------- // Register file association

// Exename: The extension to be detected (for example: ".txt")

// Appname: The application name to be associated (for example: "c: /myapp/myapp.exe")

// appkey: Exename extension key value in the registry (for example: "txtfile")

// icon: Extension icon file named Exename (for example: "c: /myapp/myapp.exe, 1")

// Describe: File Type Description

Void __fastcall registerfilerelation (Ansistring EXTNAME, ANSISTRING Appname,

Ansistring AppKey, ANSISTRING ICON, ANSISUSTRING DESCRIBE

{

Try

{

Tregistry * preg = new tregistry ();

preg-> rootkey = hkey_classes_root; // Open the primary key

// New / open the item named file extension, and write data

preg-> OpenKey (EXTNAME, TRUE);

Preg-> WriteString (", appkey);

Preg-> CloseKey ();

Preg-> OpenKey (AppKey, True);

Preg-> Writestring ("", Describe);

Preg-> CloseKey ();

Preg-> OpenKey (AppKey "// default", true);

Preg-> Writestring (", icon);

Preg-> CloseKey ();

Preg-> OpenKey (AppKey "// shell", TRUE);

Preg-> Writestring ("", "open");

Preg-> CloseKey ();

Preg-> OpenKey (AppKey "// shell // open // command", true);

Preg-> Writestring ("", AppName "% 1");

Preg-> CloseKey ();

DELETE PREG;

}

Catch (...)

{

MessageBox (NULL, "error occurred while registering files", "Note", MB_OK | MB_ICONWARNING);

}

}

The above code I compiled in BCB6 Windows 2000. Welcome everyone to communicate, please support my homepage: http://www.maxss.net

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

New Post(0)