كود:
Dim MyKey As WshShell
Set MyKey = New WshShellSet
'اسم نوع الملف
MyKey.RegWrite "HKEY_CLASSES_ROOT\Test Ext\", "Test Ext", "REG_SZ"
'الأمر المراد تنفيذه لفتح هذا النلوع من الملفات
MyKey .RegWrite "HKEY_CLASSES_ROOT\Test Ext\shell\open\command\", "C:\Test.exe %1", "REG_SZ"
' الأيقونة المختارة لتمثيل هذا النوع من الملفات, في هذا المثال اخترنا الأيقونة الثالثة المحتواة في Test.exe
'الأيقونة الأولى تحمل الـ index 0
MyKey.RegWrite "HKEY_CLASSES_ROOT\Test Ext\DefaultIcon\", "C:\Test.exe,2", "REG_SZ"
""
'اللاحقة الموافقة لهذا النوع من الملفات
MyKey .RegWrite "HKEY_CLASSES_ROOT\.jmr\", "Test Ext", "REG_SZ"
باستعمال دوال الـ API
كود:
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA"(ByVal hKey As Long, ByVal lpSubKey As String,phkResult As Long) As Long
Private Declare Function RegSetValue Lib "advapi32.dll" Alias "RegSetValueA"(ByVal hKey As Long, ByVal lpSubKey As String,ByVal dwType As Long, ByVal lpData As String,ByVal cbData As Long) As Long
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const MAX_PATH = 255
Private Const REG_SZ = 1
Private Sub cmdAssocExt_Click()
Dim MyKey As Long
Call RegCreateKey(HKEY_CLASSES_ROOT, "Test Ext", MyKey )
Call RegSetValue&(MyKey , "", REG_SZ, "Test Ext", 0&)
Call RegSetValue&(MyKey , "shell\open\command", REG_SZ, "C:\Test.exe %1", MAX_PATH)
Call RegSetValue&(MyKey , "DefaultIcon", REG_SZ, "C:\Test.exe,2", MAX_PATH)
Call RegCreateKey&(HKEY_CLASSES_ROOT, ".ext", MyKey )
Call RegSetValue&(MyKey , "", REG_SZ, "Test Ext", 0&)
End Sub