讓自己的軟體實現雙擊開啟檔案
作者:帥宏軍
unit shjAssociateFileType;
interface
uses Windows, Registry;
{將檔案類型strFileExtension與程式strExeFileName相關聯,strDiscription為檔案類型說明}
function AssignToProgram(const strFileExtension, strDiscription, strExeFileName: string ): boolean;
implementation
{將檔案類型strFileExtension與程式strExeFileName相關聯,strDiscription為檔案類型說明}
function AssignToProgram(const strFileExtension, strDiscription, strExeFileName: string ): boolean;
var
regFile: TRegistry;
begin
//建立一個Registry執行個體
regFile := TRegistry.Create;
with regFile do
begin
//設定根索引值為HKEY_CLASSES_ROOT
RootKey := HKEY_CLASSES_ROOT;
//建立或者開啟副檔名自動關聯註冊鍵
OpenKey( '.' + strFileExtension, true);
//設定副檔名自動關聯
WriteString('', strFileExtension + '_Auto_File');
//關閉鍵
CloseKey;
//建立或者開啟開啟自動關聯鍵
OpenKey(strFileExtension + '_Auto_File', true);
//設定檔案類型說明
WriteString('', strDiscription);
//關閉鍵
CloseKey;
//建立或開啟關聯程式鍵
OpenKey(strFileExtension + '_Auto_File\shell\open\command', true);
//設定關聯程式(注意:%1加上雙引號,可以解決檔案路徑中含空格的問題)
WriteString('',strExeFileName + ' "%1"');
//關閉鍵
CloseKey;
//開啟預設表徵圖鍵
OpenKey(strFileExtension + '_Auto_File\defaulticon',true);
//關聯預設表徵圖
WriteString('', strExeFileName + ',0');
//釋放
Free;
Result := True;
end;
end;
end.