欢迎来到学术参考网

如何在 Windows操作系统中改变文件打开方式

发布时间:2015-07-09 11:03

关键词:

在Windows 95/NT/98操作系统中改变文件打开方式的问题,又可称为改变文件类型关联的问题,即把某类型(扩展名)的文件与某应用程序关联,例如通常当双击*.txt文件时系统自动调用。本文介绍利用Windows注册表编辑器手工或编程改变文件打开方式的方法,并提供程序实例。

一、基本思路:

  1、注册表编辑器是用于更改系统注册表设置的高级工具,包含了关于系统配置及运行的重要信息,默认访问路径为C:“Windows“。双击图标,运行注册表编辑器。在左侧显示栏内看到HKEY_CLASSES_ROOT、HKEY_CURRENT_USER、HKEY_LOCAL_MACHINE等主键。与文件类型有关的所有主键、键名、键值都存放在HKEY_CLASSES_ROOT下。

  ◆双击HKEY_CLASSES_ROOT,向下拖动滚动条,找到.txt主键,右侧显示栏内“txtfile”说明:在HKEY_CLASSES_ROOT下有一txtfile主键,其下存放了打开*.txt文件应用程序的有关信息。

  ◆向下拖动滚动条,找到txtfile主键,右侧显示栏内“文本文档”为文件类型描述。双击txtfile,DefaultIcon右侧显示栏内“,-152”为*.txt文件的图标;shell“open“command,右侧显示栏内“C:“WINDOWS“ %1”为打开*.txt文件的应用程序名称及参数。

  改变打开文件方式的方法(例如用VISIO打开*.exc文件):

  ◆手工:打开系统注册表,在HKEY_CLASSES_ROOT下找到.exc及另一主键名,找到此主键,将shell“open“command右侧显示栏内“C:“WINDOWS“ %1”改为“C:“ %1”(假设的访问路径是C:“,具体视情而定),按F5刷新系统注册表。

  ◆编程:利用VB、Delphi、C++Builder等读写系统注册表,可自动改变文件打开方式。本文提供VB、Delphi编程实例。

  二、编程实例:

  ㈠利用VB编程

  1、在VB5.0 IDE中,新建工程Project1,在Form1上添加命令按钮Command1。

 2、选择菜单“工程”棥疤砑幽?椤困“模块”棥按蚩保赑roject1中添加模块Moudle1。

 

  3、在Moudle1“通用椛鳌辈糠稚鰽PI函数和常量。

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:“ 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环境中调试通过。

 

  ㈡利用Delphi编程

  1、在Delphi3.0 IDE中,新建工程Project1,在Form1上添加按钮Button1。

 2、在uses子句中添加Registry。

  3、双击Button1,编写Click事件代码。

procedure 1Click(Sender: TObject);

var

MyRegistry : TRegINIFile;

Return:string;

begin

try

MyRegistry := (‘‘);

y := HKEY_CLASSES_ROOT;

Return:=ring (‘.gid‘,‘‘,‘No! Not Found the Key!‘);

tring(Return,‘‘,‘这只是一个演示!‘);

tring(Return+‘“DefaultIcon‘,‘‘,‘c:“,1‘);

tring(Return+‘“shell“open“command‘,‘‘,‘c:“ %1‘);

finally

;

end;

ShowMessage(‘改变文件打开方式成功!‘);

end;

 

  4、按F9运行程序,在简体中文Windows95/NT/98、Delphi3.0/4.0环境中调试通过。

上一篇:Magic DaVE 4A 数字特技切换台的功能及一些实用技巧

下一篇:硬盘系统在播出领域的应用