100分请教,如何通过注册表获得应用程序的安装路径?在先等待

wangwm 2003-10-13 02:38:21
本人想做个应用程序的升级包,需要找到以前的应用程序然后加以升级,可是不知如何获得所安装程序的路径,也想用查找的方法,可是太慢了。
现请教各位如何通过注册表获得应用程序的安装路径??
...全文
535 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lionking1027 2003-10-13
  • 打赏
  • 举报
回复
UP,学习ING
yoki 2003-10-13
  • 打赏
  • 举报
回复
主要取决于你打包的时候怎么存的路径
tmxsl1214 2003-10-13
  • 打赏
  • 举报
回复
关注
abc10 2003-10-13
  • 打赏
  • 举报
回复
你先根据找到你的注册表与安装路径有关的键值,然后在你的升级程序中先读出该键值,问题应该可以搞定。
以下是读写注册表的一些模块


'读写注册表

Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_PERFORMANCE_DATA = &H80000004
Public Const ERROR_SUCCESS = 0&

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String) As Long
Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long
Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long

Private Declare Function RegEnumKeyEx Lib "advapi32.dll" Alias "RegEnumKeyExA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, lpcbName As Long, ByVal lpReserved As Long, ByVal lpClass As String, lpcbClass As Long, lpftLastWriteTime As Any) As Long
Private Declare Function RegEnumValue Lib "advapi32.dll" Alias "RegEnumValueA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpValueName As String, lpcbValueName As Long, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long

Public Const REG_SZ = 1 ' Unicode nul terminated string
Public Const REG_DWORD = 4 ' 32-bit number
'读写注册表


'读写注册表


Public Function GetString(hKey As Long, strPath As String, strValue As String)

Dim keyhand As Long
Dim datatype As Long
Dim lResult As Long
Dim strBuf As String
Dim lDataBufSize As Long
Dim intZeroPos As Integer
r = RegOpenKey(hKey, strPath, keyhand)
lResult = RegQueryValueEx(keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
intZeroPos = InStr(strBuf, Chr$(0))
If intZeroPos > 0 Then
GetString = Left$(strBuf, intZeroPos - 1)
Else
GetString = strBuf
End If
End If
End If

End Function

Public Sub SaveString(hKey As Long, strPath As String, strValue As String, strdata As String)

Dim keyhand As Long
Dim r As Long
r = RegCreateKey(hKey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
r = RegCloseKey(keyhand)

End Sub

Public Function DelString(ByVal hKey As Long, ByVal strPath As String, ByVal strValue As String)

Dim keyhand As Long
Dim r As Long
r = RegOpenKey(hKey, strPath, keyhand)
r = RegDeleteValue(keyhand, strValue)
r = RegCloseKey(keyhand)

End Function

Public Function DelKeys(ByVal hKey As Long, ByVal strPath As String) ', ByVal strKeys As String)

Dim r As Long
r = RegDeleteKey(hKey, strPath)

End Function

Public Function CreateKeys(ByVal hKey As Long, ByVal strPath As String)

Dim keyhand As Long
Dim r As Long
r = RegCreateKey(hKey, strPath, keyhand)
r = RegCloseKey(keyhand)

End Function


Public Function ListSubkey(ByVal hKey As Long, ByVal strPath As String) As String

Dim keyhand As Long
Dim r As Long
Dim strlistsubkey As String

Dim sName As String, sData As String, Ret As Long, RetData As Long
Dim indexs As Long

Const ERROR_NO_MORE_ITEMS = 259&
Const BUFFER_SIZE As Long = 255
Ret = BUFFER_SIZE
sName = Space(BUFFER_SIZE)

r = RegOpenKey(hKey, strPath, keyhand)
While RegEnumKeyEx(keyhand, indexs, sName, Ret, ByVal 0&, vbNullString, ByVal 0&, ByVal 0&) <> ERROR_NO_MORE_ITEMS
If strlistsubkey = "" Then
strlistsubkey = Left$(sName, Ret)
Else
strlistsubkey = strlistsubkey + "_" + Left$(sName, Ret)
End If
indexs = indexs + 1
Wend
ListSubkey = strlistsubkey
End Function

'读写注册表



abc10 2003-10-13
  • 打赏
  • 举报
回复
一般在这里可以找到你的程序名和键值。
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\

你也可以搜索一下你的程序安装时建立的快捷方式,根据快捷方式确定所在路径
wangwm 2003-10-13
  • 打赏
  • 举报
回复
是这样的,我以前的程序是用install shield打的包,所有的操作在注册表里都有记录,在注册表中能看到所注册的组件,当然包括路径了,比如
HKEY_CLASSES_ROOT\CLSID\{01B576C7-98FA-463C-8D2B-0323053ECA59}\InprocServer32等等

如果各位老大有什么代码,请mail:wangwm@anhuinews.com
davidlv 2003-10-13
  • 打赏
  • 举报
回复
'------------------------------
'作者:David_Lv
'时间:2003-06-08
'
'类名:CRegister
'功能:对注册表的一些简单操作
'------------------------------
Option Explicit


'======变量定义======
Private Const HKEY_CURRENT_USER = &H80000001 '所需注册的hKey位置
Private Const REG_SZ = 1 '键值的数据类型


'======API声明======
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

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 RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String) As Long

Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long

Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
(ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
lpType As Long, lpData As Any, lpcbData As Long) As Long

Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
(ByVal hKey As Long, ByVal lpSubKey As String) As Long


'======方法定义======

'创建一个项
Public Function CreateRegKey(ByVal vSubKey As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long

'检查传入的值是否合法
If Len(vSubKey) = 0 Then GoTo ErrH

'在注册表中创建一个项
nResult = RegCreateKey(HKEY_CURRENT_USER, vSubKey, nResult)
If nResult <> 0 Then GoTo ErrH
CreateRegKey = True
Exit Function

ErrH:
CreateRegKey = False
End Function


'删除一个最底层项
Public Function DeleteRegKey(ByVal vSubKey As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long

'检查传入的值是否合法
If Len(vSubKey) = 0 Then GoTo ErrH

'在注册表中删除一个项
nResult = RegDeleteKey(HKEY_CURRENT_USER, vSubKey)
If nResult <> 0 Then GoTo ErrH
DeleteRegKey = True
Exit Function

ErrH:
DeleteRegKey = False
End Function


'在指定的项中写入一个值
Public Function SetRegValue(ByVal vSubKey As String, ByVal vKeyName As String, ByVal vKeyValue As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long
Dim nKey As Long

'检查传入的值是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
If Len(vKeyName) = 0 Then GoTo ErrH
If Len(vKeyValue) = 0 Then GoTo ErrH

'检查是否存在vSubKey项,成功会返回一个nKey值
nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey)
If nResult <> 0 Then GoTo ErrH

'根据nKey键值,写入键名及键值
SetRegValue = RegSetValueEx(nKey, vKeyName, 0, REG_SZ, ByVal vKeyValue, LenB(StrConv(vKeyValue, vbFromUnicode)) + 1)
If SetRegValue <> 0 Then GoTo ErrH
SetRegValue = True
Exit Function

ErrH:
SetRegValue = False
End Function


'查找指定项中指定键值名称的键值
'如果注册表中没有指定的项值vSubKey,lpcbData参数返回0
'如果有项值但没有键名vKeyName,lpcbData参数返回2
'如果vKeyValue为一个字符,lpcbData参数返回2
'如果vKeyValue大于一个字符,lpcbData参数返回>2
Public Function GetRegValue(ByVal vSubKey As String, ByVal vKeyName As String) As String
On Error GoTo ErrH
Dim nResult As Long, nKey As Long, nKeyValue As String
Dim nBufferSize As Long

'检查传入的参数是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
If Len(vKeyName) = 0 Then GoTo ErrH

'打开指定的项,成功会返回一个nKey值
nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey)
If nResult <> 0 Then GoTo ErrH

'根据nKey获取键值的长度
nResult = RegQueryValueEx(nKey, vKeyName, 0, REG_SZ, 0, nBufferSize)


'根据nKey获取键值的内容
nKeyValue = Space(nBufferSize + 1)
nResult = RegQueryValueEx(nKey, vKeyName, 0, REG_SZ, ByVal nKeyValue, nBufferSize)

'返回的nKeyValue的后面会有Null字符,所以先用""替换Null字符,再进行Trim
nKeyValue = Trim(Replace(nKeyValue, vbNullChar, ""))
GetRegValue = nKeyValue
Exit Function

ErrH:
GetRegValue = ""
End Function


'删除指定项中的一个键
Public Function DeleteRegValue(ByVal vSubKey As String, ByVal vKeyName As String) As Boolean
On Error GoTo ErrH
Dim nResult As Long
Dim nKey As Long

'检查传入的参数是否合法
If Len(vSubKey) = 0 Then GoTo ErrH
If Len(vKeyName) = 0 Then GoTo ErrH

'打开指定的项,成功会返回一个nKey值
nResult = RegOpenKey(HKEY_CURRENT_USER, vSubKey, nKey)
If nResult <> 0 Then GoTo ErrH

'根据nKey删除指定的键
nResult = RegDeleteValue(nKey, vKeyName)
If nResult <> 0 Then GoTo ErrH
DeleteRegValue = True
Exit Function

ErrH:
DeleteRegValue = False
End Function




以上这些是用于进行注册表操作的代码,可以写成一个类或模块
下面是另外的代码,是界面操作的
Option Explicit


'======注册表所需用的参数
Private Const mSubKey = "SOFTWARE\EMIS\ServerInfo" '默认注册的项名
Private Const mDBServerKeyName = "DBServerName" '默认数据库注册键名
Private Const mCOMServerKeyName = "COMServerName" '默认组件注册键名
Private mDBServerName As String '数据库服务器名
Private mCOMServerName As String '组件服务器名


'初始化
Private Sub Form_Load()
Dim nRegister As New clsRegister

'从注册表中读取注册值
txtDBServer.Text = nRegister.GetRegValue(mSubKey, mDBServerKeyName)
txtCOMServer.Text = nRegister.GetRegValue(mSubKey, mCOMServerKeyName)

Set nRegister = Nothing
End Sub
davidlv 2003-10-13
  • 打赏
  • 举报
回复
你以前的程序安装时,应该在注册表里写有安装路径,更新时通过注册表读写程序来做,如果你只是没有这些程序代码,我可以给你,但如果你以前的程序里没有写注册表,那没办法了
abc10 2003-10-13
  • 打赏
  • 举报
回复
你用什么打的包。
主要取决于你打包时有没有在注册表里写安装路径。
wangwm 2003-10-13
  • 打赏
  • 举报
回复
以前的程序是在注册表中有安装路径
goodsgy 2003-10-13
  • 打赏
  • 举报
回复
以前的程序是不是在注册表中有安装路径阿

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧