Assoc文件关联宏

优质
小牛编辑
134浏览
2023-12-01
!include quot;logiclib.nsh"
!ifndef Project
!define Project AssocTool   ;改成你的软件名称
!endif

;关联文件,用法:!insertmacro Assoc dts quot;dts" "DVD 文件" "MCP.exe" "MCP.ico"
!macro Assoc EXT TYPE DESC OPENEXE ICO
Push $0
Push $1
Push $2
Push $3
StrLen $3 quot;${Project}quot;
StrCpy $2 $1 $3
${if} $2 != quot;${Project}quot;
WriteRegStr HKCR quot;Back.${Project}\.${ext}quot; "" "$1quot;   ;备份该扩展名
${EndIf}

${if} $1 == quot;"
DetailPrint '$(Associatedfiletypes) *.${ext} $(For) quot;${desc}quot;...'
WriteRegStr HKCR quot;Back.${Project}\.${ext}quot; "" "_Blank_"
${Else}
DetailPrint '$(Modifyfiletypes) *.${ext} $(For) quot;${desc}quot;...'
${EndIf}
;关于基本关联的
WriteRegStr HKCR quot;.${ext}quot; "" "${Project}.${type}quot;
WriteRegStr HKCR quot;${Project}.${type}quot; "" "${desc}quot;
WriteRegStr HKCR quot;${Project}.${type}\shellquot; "" "open"
WriteRegStr HKCR quot;${Project}.${type}\shell\openquot; "" "$(PLAY)quot;
WriteRegStr HKCR quot;${Project}.${type}\shell\open\commandquot; "" '${openexe} quot;%1"'
WriteRegStr HKCR quot;${Project}.${type}\DefaultIconquot; "" "${ico}quot;
;标记有关联文件(只要非空即可)
WriteRegStr HKCR quot;Back.${Project}quot; "" "${Project} Backupquot;
Pop $3
Pop $2
Pop $1
!macroend

;取消关联,用法:!insertmacro UnAssoc dts
!macro UnAssoc EXT

Push $1
Push $2  ;Content Type
Push $3  ;CLSID
;**********修改以下代码要谨慎!**********

ReadRegStr $1 HKCR quot;Back.${Project}\.${EXT}quot; ""     ;读备份
ReadRegStr $2 HKCR quot;.${EXT}quot; ""                 ;读现在的

StrLen $3 ${Project}
StrCpy $4 $2 $3

${if} quot;$4quot; == quot;${Project}quot;        ;现在是不是关联了
${if} quot;$1quot; == quot;_Blank_"  ;如果备份有quot;_Blank_",证明已经关联,但没有旧类型.
    WriteRegStr HKCR quot;.${EXT}quot; "" ""           ;不作处理
${Else}
    ${if} quot;$1quot; != quot;"
    WriteRegStr HKCR quot;.${EXT}quot; "" "$1quot;         ;恢复备份
    ${EndIf}
${EndIf}
DeleteRegKey HKCR quot;Back.${Project}\.${ext}quot;   ;恢复完成,删除备份
StrCmp quot;${Project}.${ext}quot; "." +2
DeleteRegKey HKCR quot;${Project}.${ext}quot;   ;恢复完成,删除备份
${EndIf}
Pop $3
Pop $2
Pop $1
!macroend

原始作者:Ansifa