Non compatible Windows 98.
Suite à un problème de bug dont tolf à subit les inconvénients , J'ai testé ce code à plusieurs reprise, mais prenez soins de sauvegarder votre base de registre avant de le tester dans tous les sens (Juste au cas ou ...).
Fichier _FiletypeAssociation.au3 :
Code : Tout sélectionner
#include-once
;====================================================================================================================
; Name : _FiletypeAssociation
; Description : Make association of a file
; Syntax : _FiletypeAssociation($Extension, [$DocumentType, [$Program, [$Description, [$IconFile, [$IconIndex, , [$OverwriteExt]]]]])
; Parameter(s) : $Extension - The extension of file to associate.
; $DocumentType - The name of the document type (Nothing for deleting association).
; $Program - The programme path and name to associate with (Nothing for deleting association).
; $Description - The description of document file (Default $Extension.Document).
; $IconFile - The path and name of icon file (Default $Program icon).
; $IconIndex - The index number of icon in file (Default 0).
; $OverwriteExt - Indicates if you want to overwrite extension association (Default 1)
; Requirement(s) : Win32
; Return value(s) : Return 1 - On Success.
; Return 0 - On Failure (@error and @extented are set).
; @extented 1 - The program to associate doesn't exists.
; @extented 2 - On association deletion if file extension is not exist.
; @extented 3 - Extention allready exists and $OverwriteExtension = 0.
;
; Author : Tlem <tlem at tuxolem dot net>
; Note(s) : None
; ===============================================================================================================================
Func _FiletypeAssociation($Extension, $DocumentType = "", $Program = "", $Description = "", $IconFile = "", $IconIndex = 0, $OverwriteExt = 1)
; If $Extension don't have the left dot, we add it.
If StringLeft($Extension, 1) <> "." Then $Extension = "." & $Extension
; Check valid Extention.
If Not _CheckExtension($Extension) Then Return 0
$Err = 0
; Delete extension ($DocumentType and $Program equal "").
If $DocumentType = "" And $Program = "" Then
; Read document type.
$DocumentType = RegRead("HKCR\" & $Extension, "")
; To return that the key doesn't exist anymore.
If Not RegRead("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & $Extension, "") _
And Not RegRead("HKCR\" & $Extension, "") Then
SetError(1, 2)
Return 0
Else
; Delete extension in registry and use ftype.exe to un-associate extension.
RunWait(@ComSpec & " /c assoc " & $Extension & "=" & "nothing", "", @SW_HIDE)
; Make control before deletion
If Not $Extension = "" Then
RegDelete("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\" & $Extension)
RegDelete("HKCR\" & $Extension)
EndIf
; Make control before deletion
If Not $DocumentType = "" Then RegDelete("HKCR\" & $DocumentType)
If RegRead("HKCR\" & $DocumentType, "") = "" And RegRead("HKCR\" & $Extension, "") = "" Then $Err = 1
EndIf
Else
; Write extension :
; If $Program not exists then return error.
If Not FileExists($Program) Then
SetError(1, 1)
Return 0
ElseIf RegRead("HKCR\" & $Extension, "") And $OverwriteExt = 0 Then
SetError(1, 3)
Return 0
Else
; If $DocumentType is empty, use extention and ".Document".
If $DocumentType = "" Then $DocumentType = $Extension & ".Document"
; If name of icon file is empty, use icone of the associted program.
If $IconFile = "" Then $IconFile = $Program
; Write extension in registry and use ftype.exe to make association.
RunWait(@ComSpec & " /c ftype " & $DocumentType & '="' & $Program & '" "%1" && assoc ' & $Extension & "=" & $DocumentType, "", @SW_HIDE)
RegWrite("HKCR\" & $DocumentType, "", "Reg_sz", $Description)
RegWrite("HKCR\" & $DocumentType & "\DefaultIcon", "", "REG_SZ", $IconFile & "," & $IconIndex)
If RegRead("HKCR\" & $DocumentType & "\Shell\Open\Command", "") <> "" And RegRead("HKCR\" & $Extension, "") = $DocumentType Then $Err = 1
EndIf
EndIf
Return $Err
EndFunc ;==>_FiletypeAssociation
Func _CheckExtension($Extension)
; A normal extention is 2 or 3 chars long so with the dot it is 3 or 4 chars.
If StringLen($Extension) < 3 Or StringLen($Extension) > 4 Then Return 0
; If Extention is in string => error.
If StringInStr(".exe .bat .dll .dir .lnk", $Extension) Then Return 0
; Check for unauthorized chars in extention.
For $i = 1 To StringLen($Extension)
$s_CurChar = StringMid($Extension, $i, 1)
If Not StringRegExp($s_CurChar, '[.~_a-zA-Z]') Then Return 0
Next
Return 1
EndFunc ;==>_CheckExtension
Code : Tout sélectionner
; Méthode simple :
;Association des fichiers .abc avec Scite (Icône Scite)
; Le nom de l'extention peut être : ".abc" ou "abc" (Au choix).
If _FiletypeAssociation(".abc", "abc.Document", "%ProgramFiles%\AutoIt3\SciTE\SciTE.exe") Then
; Méthode évoluée :
; Association des fichiers .abc avec Scite (Icône %ProgramFiles%\AutoIt3\Icons\filetype3.ico)
_FiletypeAssociation(".abc", "abc.Document", "%ProgramFiles%\AutoIt3\SciTE\SciTE.exe", "", "%ProgramFiles%\AutoIt3\Icons\filetype3.ico")
MsgBox(64, "Info", "l'association c'est bien déroulée")
Else
MsgBox(16, "Erreur", "Erreur lors de l'association des fichiers")
EndIf
; Suppression de l'association des fichiers .abc
If _FiletypeAssociation(".abc") Then ; Il est possible de ne pas mettre le point
MsgBox(64, "Info", "La suppression de l'association c'est bien déroulée")
Else
MsgBox(16, "Erreur", "Erreur lors de la suppression de l'association")
EndIf