菜鸟捉虫(8)

软件世界

当我们双击“.txt”文件时,系统会自动以记事本程序打开它,这秘密放在注册表中。如果熟悉注册表,我们可知道与.txt有关的一些数据如下:
[HKCR\.txt]
(默认)="txtfile"
Content Type="text/plain"
[HKCR\txtfile]
(默认)="文本文档"
[HKCR\txtfile\DefaultIcon]
(默认)="shell32.dll,-152"
[HKCR\txtfile\shell]
(默认)="open"
[HKCR\txtfile\shell\open\command]
(默认)="Notepad %1"

本期题目:

新建一个文件类型(.wj),让该类型与记事本建立关联,所以编制的程序就应该在注册表中建立如上的键值。用VB编写程序如下,但程序有BUG,请你改正。
Dim hKey As Long, fileName As String
Dim NotepadDir As String, wjIcon As String
wjIcon = "shell32.dll,-152"
fileName = "Notepad.exe %1"
hKey = HKEY_CLASSES_ROOT
RegSetValue hKey, ".wj", REG_ BINARY, "wjtext", 6
RegSetValue hKey, "wjtext", REG_ BINARY, "wj文档", 6
RegSetValue hKey, "wjtext\shell", REG_BINARY, "open", 4
RegSetValue hKey, "wjtext\shell\open\command", REG_ BINARY, _
fileName, LenB(StrConv(fileName, vbFromUnicode))
RegSetValue hKey, "wjtext\DefaultIcon", REG_ BINARY, _
wjIcon, LenB(StrConv(wjIcon, vbFromUnicode))
RegCloseKey hKey