在Windows 95/NT/98操作系统中改变文件打开方式的问题,又可称为改变文件类型关联的问题,即把某类型(扩展名)的文件与某应用程序关联,例如通常当双击*.txt文件时系统自动调用Notepad.exe。本文介绍利用Windows注册表编辑器Regedit.exe手工或编程改变文件打开方式的方法,并提供程序实例。 一、基本思路: 1、注册表编辑器Regedit.exe是用于更改系统注册表设置的高级工具,包含了关于系统配置及运行的重要信息,默认访问路径为C:\Windows\Regedit.exe。双击Regedit.exe图标,运行注册表编辑器。在左侧显示栏内看到HKEY_CLASSES_ROOT、KEY_CURRENT_USER、HKEY_LOCAL_MACHINE等主键。与文件类型有关的所有主键、键名、键值都存放在HKEY_CLASSES_ROOT下。 ◆双击HKEY_CLASSES_ROOT,向下拖动滚动条,找到.txt主键,右侧显示栏内“txtfile”说明:在HKEY_CLASSES_ROOT下有一txtfile主键,其下存放了打开*.txt文件应用程序的有关信息。 ◆向下拖动滚动条,找到txtfile主键,右侧显示栏内“文本文档”为文件类型描述。双击txtfile,DefaultIcon右侧显示栏内“shell32.dll,-152”为*.txt文件的图标;shell\open\command,右侧显示栏内“C:\WINDOWS\NOTEPAD.EXE %1”为打开*.txt文件的应用程序名称及参数。 改变打开文件方式的方法(例如用VISIO打开*.exc文件): ◆手工:打开系统注册表,在HKEY_CLASSES_ROOT下找到.exc及另一主键名,找到此主键,将shell\open\command右侧显示栏内“C:\WINDOWS\NOTEPAD.EXE %1”改为“C:\VISIO.EXE %1”(假设VISIO.EXE的访问路径是C:\,具体视情况而定),按F5刷新系统注册表。 ◆编程:利用VB、Delphi、C++Builder等读写系统注册表,可自动改变文件打开方式。本文提供VB、Delphi编程实例。 二、编程实例: ㈠利用VB编程 1、在VB5.0 IDE中,新建工程Project1,在Form1上添加命令按钮Command1。 2、选择菜单“工程”—“添加模块”—“模块”—“打开”,在Project1中添加模块Moudle1。 3、在Moudle1“通用—声明”部分声明API函数和常量。 Const REG_SZ = 1 Global Const HKEY_CLASSES_ROOT = &H80000000 Declare Function OSRegQueryValueEx Lib “advapi32”Alias “RegQueryValueExA”(ByVal hKey As Long, ByVal lpszValueName As String, ByVal dwReserved As Long, lpdwType As Long, lpbData As Any, cbData As Long) As Long Declare Function OSRegOpenKey Lib “advapi32”Alias “RegOpenKeyA”(ByVal hKey As Long, ByVal lpszSubKey As String, phkResult As Long) As Long Declare Function OSRegSetValueEx Lib“advapi32”Alias “RegSetValueExA”(ByVal hKey As Long, ByVal lpszValueName As String, ByVal dwReserved As Long, ByVal fdwType As Long, lpbData As Any, ByVal cbData As Long) As Long Declare Function OSRegCloseKey Lib“advapi32”Alias “RegCloseKey”(ByVal hKey As Long) As Long 4、在Moudle 1中编写函数。 Function RegOpenKey(ByVal hKey As Long, ByVal lpszSubKey As String, phkResult As Long) As Boolean Dim lResult As Long On Error GoTo 0 ` 关闭错误陷阱 lResult = OSRegOpenKey(hKey, lpszSubKey, phkResult) If lResult = 0 Then RegOpenKey = True Else RegOpenKey = False End If End Function Function RegSetStringValue(ByVal hKey As Long, ByVal strValueName As String, ByVal strData As String, Optional ByVal fLog) As Boolean Dim lResult As Long On Error GoTo 0 lResult = OSRegSetValueEx(hKey, strValueName, 0&, REG_SZ, ByVal strData, LenB(StrConv(strData, vbFromUnicode)) + 1) If lResult = 0 Then RegSetStringValue = True Else RegSetStringValue = False End If End Function Function StripTerminator(ByVal strString As String) As String Dim intZeroPos As Integer intZeroPos = InStr(strString, Chr$(0)) If intZeroPos > 0 Then StripTerminator=Left$(strString, intZeroPos - 1) Else StripTerminator = strString End If End Function Function RegQueryStringValue(ByVal hKey As Long, ByVal strValueName As String, strData As String) As Boolean Dim lResult As Long Dim lValueType As Long Dim strBuf As String Dim lDataBufSize As Long RegQueryStringValue = False On Error GoTo 0 lResult = OSRegQueryValueEx(hKey, strValueName, 0&, lValueType, ByVal 0&, lDataBufSize) If lResult = ERROR_SUCCESS Then If lValueType = REG_SZ Then strBuf = String(lDataBufSize, “”) lResult = OSRegQueryValueEx(hKey, strValueName, 0&, 0&, ByVal strBuf, lDataBufSize) If lResult = ERROR_SUCCESS Then RegQueryStringValue = True strData = StripTerminator(strBuf) End If End If End If End Function 5、双击Command1,编写Click事件代码。 Private Sub Command1_Click() Dim hKey As Long Dim MyReturn As Long Dim MyData As String MyReturn = OSRegOpenKey(HKEY_CLASSES_ROOT, “.exc”, hKey) MyReturn=RegQueryStringValue(hKey,“”,MyData) MyReturn=OSRegOpenKey(HKEY_CLASSES_ROOT, MyData+“\shell\open\command”,hKey) MyReturn = RegSetStringValue(hKey,“”,“c:\visio.exe 1%”, False) If MyReturn Then MsgBox “改变文件打开方式成功!”,vbInformation,“请注意” Else MsgBox “改变文件打开方式失败!”,vbExclamation,“请注意” End If OSRegCloseKey (hKey) End Sub 6、按F5运行程序,在简体中文Windows95/NT/98、VB5.0/6.0环境中调试通过。 |