We don't display ads so we rely on your Bitcoin donations to 1KWEk9QaiJb2NwP5YFmR24LyUBa4JyuKqZ
Post date: Mar 16, 2010 1:44:30 PM
A file association associates a file with an application capable of opening that file. More commonly, a file association associates a class of files (usually determined by their filename extension, such as .txt
) with a corresponding application (such as a text editor).
Read more: http://en.wikipedia.org/wiki/File_association
This function does not require the TRegistry unit to be added to the project since it uses pure Windows API.
{
Coder: steve10120
Compiled : Delphi 7
Website: hackhound.org
}
function RegisterFileAssoc(Ext:string; sPath:string; sName:string; sDesc:string):Boolean;
var
hOpen: HKEY;
begin
sName := sName + '.';
Ext := UpperCase(Ext);
if RegCreateKeyEx(HKEY_CLASSES_ROOT, PChar(sName + Ext), 0, nil, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, hOpen, nil) = ERROR_SUCCESS then
begin
RegSetValueEx(hOpen, nil, 0, REG_SZ, PChar(sDesc), Length(sDesc));
if RegCreateKeyEx(HKEY_CLASSES_ROOT, PChar(sName + Ext + '\DefaultIcon'), 0, nil, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, hOpen, nil) = ERROR_SUCCESS then
begin
RegSetValueEx(hOpen, nil, 0, REG_SZ, PChar(sPath), Length(sPath));
if RegCreateKeyEx(HKEY_CLASSES_ROOT, PChar(sName + Ext + '\shell\open\command'), 0, nil, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, hOpen, nil) = ERROR_SUCCESS then
begin
sPath := '"' + sPath + '" "%1"';
RegSetValueEx(hOpen, nil, 0, REG_SZ, PChar(sPath), Length(sPath));
if RegCreateKeyEx(HKEY_CLASSES_ROOT, PChar('.' + Ext), 0, nil, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, hOpen, nil) = ERROR_SUCCESS then
begin
RegSetValueEx(hOpen, nil, 0, REG_SZ, PChar(sName + Ext), Length(sName + Ext));
CloseHandle(hOpen);
if RegCreateKeyEx(HKEY_CURRENT_USER, PChar('Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.'+LowerCase(Ext)), 0, nil, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, hOpen, nil) = ERROR_SUCCESS then
begin
RegSetValueEx(hOpen, PChar('Progid'), 0, REG_SZ, PChar(sName + Ext), Length(sName + Ext));
CloseHandle(hOpen);
SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nil, nil) ;
end;
end;
end;
end;
end;
end;
Usage
RegisterFileAssoc('xxx', ParamStr(0), 'Test', 'Test File');