Page 1 of 1

File Associate

Posted: Sun Jun 29, 2003 7:25 pm
by GPI

Code: Select all

Procedure SetKey(fold,Key$,Subkey$,Type,Adr,len)
  If RegCreateKeyEx_(fold, Key$, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, 0, @NewKey, @KeyInfo) = #ERROR_SUCCESS
    RegSetValueEx_(NewKey, Subkey$, 0, Type,  Adr, len)
    RegCloseKey_(NewKey)
  EndIf
EndProcedure
Procedure AssociateFileEx(ext$,ext_description$,programm$,Icon$,prgkey$,cmd_description$,cmd_key$)
  cmd$=Chr$(34)+programm$+Chr$(34)+" "+Chr$(34)+"%1"+Chr$(34)
  If GetVersion_() & $ff0000  ; Windows NT/XP
    SetKey(#HKEY_CLASSES_ROOT, "Applications\"+prgkey$+"\shell\"+cmd_description$+"\command","",#reg_sz    ,@cmd$,Len(cmd$)+1)
    If ext_description$
      Key$=ext$+"_auto_file"
      SetKey(#HKEY_CLASSES_ROOT  ,"."+ext$           ,"",#reg_sz,@Key$,Len(Key$)+1)
      SetKey(#HKEY_CLASSES_ROOT  ,Key$               ,"",#reg_sz,@ext_description$,Len(ext_description$)+1)
      If Icon$
        SetKey(#HKEY_CLASSES_ROOT,Key$+"\DefaultIcon","",#reg_sz,@Icon$,Len(Icon$)+1)
      EndIf
    EndIf
    SetKey(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ext$,"Application",#reg_sz,@prgkey$         ,Len(prgkey$)+1)
  Else ;Windows 9x
    SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\."+ext$                        ,"",#reg_sz,@prgkey$         ,Len(prgkey$)+1)
    If ext_description$
      SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$                   ,"",#reg_sz,@ext_description$,Len(ext_description$)+1)
    EndIf
    If Icon$
      SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$+"\DefaultIcon"    ,"",#reg_sz,@Icon$           ,Len(Icon$)+1)
    EndIf
    If cmd_description$<>cmd_key$
      SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$+"\shell\"+cmd_key$,"",#reg_sz,@cmd_description$,Len(cmd_description$)+1)
    EndIf
    SetKey(#HKEY_LOCAL_MACHINE,"Software\Classes\"+prgkey$+"\shell\"+cmd_key$+"\command","",#reg_sz,@cmd$   ,Len(cmd$)+1)
  EndIf
EndProcedure
Procedure AssociateFile(ext$,ext_description$,programm$,Icon$)
  AssociateFileEx(ext$,ext_description$,programm$,Icon$,GetFilePart(programm$),"open","open")  
EndProcedure

;little Example
AssociateFile("pb","Pure Basic Source","C:\Programme\PureBasic\PureBasic.exe","F:\icon.bmp,0")
;and now an addition context-item
AssociateFileEx("pb","","F:\alternate rich\jaPBe.exe","","PureBasic.exe","Open with jaPBe","open_with_japbe")
EDIT Forget a "." in win9x part.

GPI

Posted: Mon Jul 07, 2003 10:38 pm
by Karbon
GPI, you're about the most handy guy ever. Million thanks for this!

Posted: Mon Mar 13, 2006 9:10 am
by Progi1984
But how do you un-associate file and its extension ?

Posted: Mon Mar 13, 2006 10:03 am
by ts-soft
Progi1984 wrote:But how do you un-associate file and its extension ?
see in the includes of jaPBe: [Registry.pbi]

Code: Select all

Procedure Remove_AssociateFile(ext$,prgkey$) 
  If GetVersion_() & $ff0000 ; Windows NT/XP 
    Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,"Applications"+prgkey$,"")
    Key$=ext$+"_auto_file" 
    Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,"."+ext$,"")
    Reg_DeleteKeyWithAllSub(#HKEY_CLASSES_ROOT,Key$,"")
    Reg_DeleteKeyWithAllSub(#HKEY_CURRENT_USER,"Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\."+ext$,"") 
  Else ;Windows 9x 
    Reg_DeleteKeyWithAllSub(#HKEY_LOCAL_MACHINE  ,"Software\Classes\."+ext$,"")
    Reg_DeleteKeyWithAllSub(#HKEY_LOCAL_MACHINE,"Software\Classes"+prgkey$,"")
  EndIf 
EndProcedure 

Posted: Mon Mar 13, 2006 10:32 am
by Progi1984
Thanks

Posted: Mon Mar 20, 2006 11:12 pm
by Progi1984
Just after test, the extension is not more associated, but the icon is again on file. Why that ? how can i remove that ?

Posted: Tue Mar 21, 2006 12:04 am
by ts-soft
Use the MS PowerToys Tweak UI > Repair Rebuild Icon.
I think is a Cache issue

Re: File Associate

Posted: Sat Feb 27, 2010 2:48 am
by kenmo
(Resurrecting a dinosaur of a thread.....)

Does anyone know if this registry code will work with Vista / Win7 ?
All I know is it works for me on XP Pro.

Also I cleaned up the formatting a bit and made it EnableExplicit friendly:

Code: Select all

Procedure SetKey(Fold.i, Key.s, Subkey.s, Type.i, Adr.i, len.i)
  Protected NewKey.i, KeyInfo.i
  
  If RegCreateKeyEx_(fold, Key, 0, 0, #REG_OPTION_NON_VOLATILE, #KEY_ALL_ACCESS, 0, @NewKey, @KeyInfo) = #ERROR_SUCCESS
    RegSetValueEx_(NewKey, Subkey, 0, Type,  Adr, len)
    RegCloseKey_(NewKey)
  EndIf
EndProcedure

Procedure AssociateFileEx(ext.s, ext_description.s, programm.s, Icon.s, prgkey.s, cmd_description.s, cmd_key.s)
  Protected cmd.s, Key.s
  
  cmd = Chr(34) + programm + Chr(34) + " " + Chr(34) + "%1" + Chr(34)
  If GetVersion_() & $FF0000 ; Windows NT/XP
    SetKey(#HKEY_CLASSES_ROOT, "Applications\" + prgkey + "\shell\" + cmd_description + "\command", "", #REG_SZ, @cmd, Len(cmd) + 1)
    
    If ext_description
      Key = ext + "_auto_file"
      SetKey(#HKEY_CLASSES_ROOT, "." + ext, "", #REG_SZ, @Key, Len(Key) + 1)
      SetKey(#HKEY_CLASSES_ROOT, Key, "", #REG_SZ, @ext_description, Len(ext_description) + 1)
      If Icon
        SetKey(#HKEY_CLASSES_ROOT, Key + "\DefaultIcon", "", #REG_SZ, @Icon, Len(Icon) + 1)
      EndIf
    EndIf
    
    SetKey(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\." + ext, "Application", #REG_SZ, @prgkey, Len(prgkey) + 1)
  Else ; Windows 9x
    SetKey(#HKEY_LOCAL_MACHINE, "Software\Classes\." + ext, "", #REG_SZ, @prgkey, Len(prgkey) + 1)
    
    If ext_description
      SetKey(#HKEY_LOCAL_MACHINE, "Software\Classes\" + prgkey, "", #REG_SZ, @ext_description, Len(ext_description) + 1)
    EndIf
    If Icon
      SetKey(#HKEY_LOCAL_MACHINE, "Software\Classes\" + prgkey + "\DefaultIcon", "", #REG_SZ, @Icon, Len(Icon) + 1)
    EndIf
    If cmd_description <> cmd_key
      SetKey(#HKEY_LOCAL_MACHINE, "Software\Classes\" + prgkey + "\shell\" + cmd_key, "", #REG_SZ, @cmd_description, Len(cmd_description) + 1)
    EndIf
    
    SetKey(#HKEY_LOCAL_MACHINE, "Software\Classes\" + prgkey + "\shell\" + cmd_key + "\command", "", #REG_SZ, @cmd, Len(cmd) + 1)
  EndIf
EndProcedure

Procedure AssociateFile(ext.s, ext_description.s, programm.s, Icon.s)
  AssociateFileEx(ext, ext_description, programm, Icon, GetFilePart(programm), "open", "open") 
EndProcedure

Re: File Associate

Posted: Sat Feb 27, 2010 10:30 am
by ts-soft
kenmo wrote:(Resurrecting a dinosaur of a thread.....)

Does anyone know if this registry code will work with Vista / Win7 ?
All I know is it works for me on XP Pro.
should work on 7 and xp but only in admin-mode (see compileroptions)!