查看: 1016|回复: 10
|
如何洽当地保存database的directory++再请问如何执行register.xml??
[复制链接]
|
|
假如我用VB做了一个module
是用来寻找database的directory
以便让我的program利用database...
那么
通常大家是怎样保存那个directory
以便让sub-modules来refer呢?
以前我作project的方法很烂(我是中途做project先的,现在还在读)
是在MDI form里define一个变量来保存
然后sub-module可以refer MDI
丢脸
所以要问大家比较妥当
懂VS Studio的人可以回答我...
[ 本帖最后由 haroldlbc 于 22-5-2006 01:00 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 22-5-2006 12:17 AM
|
显示全部楼层
不用丢脸, 你的算是一个方法之一. 
另一个方法, 是用 public variable.
另外, 可以记录在 registry/xml 文件里, 这样, 不只是随时随地可以 refer 你的 database, 以后再打开你的软件, 也不需要再要求用户重新输入资料库的 path. |
|
|
|
|
|
|
|
发表于 22-5-2006 09:15 AM
|
显示全部楼层
原帖由 goatstudio 于 22-5-2006 00:17 发表
不用丢脸, 你的算是一个方法之一. 
另一个方法, 是用 public variable.
另外, 可以记录在 registry/xml 文件里, 这样, 不只是随时随地可以 refer 你的 database, 以后再打开你的软件, 也不需要再要求用户重 ...
我想问,所谓不需要重新输入的意思是输入原本的path对吗?
那么,如果path换了,是不是我们需要改registry/xml里面所设定的path,对吗? |
|
|
|
|
|
|
|
发表于 22-5-2006 09:23 AM
|
显示全部楼层
原帖由 johe07 于 22-5-2006 09:15 AM 发表
我想问,所谓不需要重新输入的意思是输入原本的path对吗?
那么,如果path换了,是不是我们需要改registry/xml里面所设定的path,对吗?
这个当然... 当 path 换了... 程序找不到, pop 出对话框要求重新输入咯... |
|
|
|
|
|
|
|
发表于 22-5-2006 09:31 AM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 22-5-2006 10:13 AM
|
显示全部楼层
原帖由 goatstudio 于 22-5-2006 12:17 AM 发表
不用丢脸, 你的算是一个方法之一. 
另一个方法, 是用 public variable.
另外, 可以记录在 registry/xml 文件里, 这样, 不只是随时随地可以 refer 你的 database, 以后再打开你的软件, 也不需要再要求用户重 ...
registry/xml比较容易
public class/varable 的coding很难...
[ 本帖最后由 haroldlbc 于 22-5-2006 12:58 PM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 22-5-2006 12:59 PM
|
显示全部楼层
请问要如何执行registry/xml比较容易,
可以给sample/example吗? |
|
|
|
|
|
|
|
发表于 22-5-2006 07:36 PM
|
显示全部楼层
我猜想你用 VB6?
很久以前我这么做过... 以下的 code 是我从一个网站抄回来的, 把它弄成一个 module, 你就可以用了.
基本上, 你只需要用到:
WriteRegistry
ReadRegistry
DeleteValue
- Option Explicit
- 'Security Mask constants
- Public Const READ_CONTROL = &H20000
- Public Const SYNCHRONIZE = &H100000
- Public Const STANDARD_RIGHTS_ALL = &H1F0000
- Public Const STANDARD_RIGHTS_READ = READ_CONTROL
- Public Const STANDARD_RIGHTS_WRITE = READ_CONTROL
- Public Const KEY_QUERY_VALUE = &H1
- Public Const KEY_SET_VALUE = &H2
- Public Const KEY_CREATE_SUB_KEY = &H4
- Public Const KEY_ENUMERATE_SUB_KEYS = &H8
- Public Const KEY_NOTIFY = &H10
- Public Const KEY_CREATE_LINK = &H20
- Public Const KEY_ALL_ACCESS = ((STANDARD_RIGHTS_ALL Or KEY_QUERY_VALUE Or _
- KEY_SET_VALUE Or KEY_CREATE_SUB_KEY Or KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY Or _
- KEY_CREATE_LINK) And (Not SYNCHRONIZE))
- Public Const KEY_READ = ((STANDARD_RIGHTS_READ Or KEY_QUERY_VALUE Or _
- KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not SYNCHRONIZE))
- Public Const KEY_EXECUTE = ((KEY_READ) And (Not SYNCHRONIZE))
- Public Const KEY_WRITE = ((STANDARD_RIGHTS_WRITE Or KEY_SET_VALUE _
- Or KEY_CREATE_SUB_KEY) And (Not SYNCHRONIZE))
- ' Possible registry data types
- Public Enum InTypes
- ValNull = 0
- ValString = 1
- ValXString = 2
- ValBinary = 3
- ValDWord = 4
- ValLink = 6
- ValMultiString = 7
- ValResList = 8
- End Enum
- ' Registry value type definitions
- Public Const REG_NONE As Long = 0
- Public Const REG_SZ As Long = 1
- Public Const REG_EXPAND_SZ As Long = 2
- Public Const REG_BINARY As Long = 3
- Public Const REG_DWORD As Long = 4
- Public Const REG_LINK As Long = 6
- Public Const REG_MULTI_SZ As Long = 7
- Public Const REG_RESOURCE_LIST As Long = 8
- ' Registry section definitions
- 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 HKEY_CURRENT_CONFIG = &H80000005
- Public Const HKEY_DYN_DATA = &H80000006
- ' Codes returned by Reg API calls
- Private Const ERROR_NONE = 0
- Private Const ERROR_BADDB = 1
- Private Const ERROR_BADKEY = 2
- Private Const ERROR_CANTOPEN = 3
- Private Const ERROR_CANTREAD = 4
- Private Const ERROR_CANTWRITE = 5
- Private Const ERROR_OUTOFMEMORY = 6
- Private Const ERROR_INVALID_PARAMETER = 7
- Private Const ERROR_ACCESS_DENIED = 8
- Private Const ERROR_INVALID_PARAMETERS = 87
- Private Const ERROR_NO_MORE_ITEMS = 259
- ' Registry API functions used in this module (there are more of them)
- 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 RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult 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, ByVal lpData As String, lpcbData As Long) 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, ByVal lpData As String, lpcbData As Long) As Long
- Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey 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 RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
- Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
- Private Declare Function RegFlushKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
- Private Declare Function RegEnumKey Lib "advapi32.dll" Alias "RegEnumKeyA" (ByVal hKey As Long, ByVal dwIndex As Long, ByVal lpName As String, ByVal cbName 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 RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String) As Long
- ' Example
- ' Text1.Text = ReadRegistry(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon", "DefaultUserName")
- '
- Public Function ReadRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String) As String
- Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long, lValueLength As Long, sValue As String, td As Double, TStr1 As String, TStr2 As String, I As Variant
- On Error Resume Next
- lResult = RegOpenKey(Group, Section, lKeyValue)
- sValue = Space$(2048)
- lValueLength = Len(sValue)
- lResult = RegQueryValueEx(lKeyValue, Key, 0&, lDataTypeValue, sValue, lValueLength)
- If (lResult = 0) And (Err.Number = 0) Then
- If lDataTypeValue = REG_DWORD Then
- td = Asc(Mid$(sValue, 1, 1)) + &H100& * Asc(Mid$(sValue, 2, 1)) + &H10000 * Asc(Mid$(sValue, 3, 1)) + &H1000000 * CDbl(Asc(Mid$(sValue, 4, 1)))
- sValue = Format$(td, "000")
- End If
- If lDataTypeValue = REG_BINARY Then
- ' Return a binary field as a hex string (2 chars per byte)
- TStr2 = ""
- For I = 1 To lValueLength
- TStr1 = Hex(Asc(Mid(sValue, I, 1)))
- If Len(TStr1) = 1 Then TStr1 = "0" & TStr1
- TStr2 = TStr2 + TStr1
- Next
- sValue = TStr2
- Else
- sValue = Left$(sValue, lValueLength - 1)
- End If
- Else
- sValue = "Not Found"
- End If
- lResult = RegCloseKey(lKeyValue)
- ReadRegistry = sValue
- End Function
- ' Example
- ' WriteRegistry HKEY_CURRENT_USER, "SOFTWARE\My Name\My App", "NewSubKey", ValString, "NewValueHere"
- ' WriteRegistry HKEY_CURRENT_USER, "SOFTWARE\My Name\My App", "NewSubKey", ValDWord, "31"
- '
- Public Sub WriteRegistry(ByVal Group As Long, ByVal Section As String, ByVal Key As String, ByVal ValType As InTypes, ByVal Value As Variant)
- Dim lResult As Long
- Dim lKeyValue As Long
- Dim InLen As Long
- Dim lNewVal As Long
- Dim sNewVal As String
- On Error Resume Next
- lResult = RegCreateKey(Group, Section, lKeyValue)
- If ValType = ValDWord Then
- lNewVal = CLng(Value)
- InLen = 4
- lResult = RegSetValueExLong(lKeyValue, Key, 0&, ValType, lNewVal, InLen)
- Else
- ' Fixes empty string bug - spotted by Marcus Jansson
- If ValType = ValString Then Value = Value + Chr(0)
- sNewVal = Value
- InLen = Len(sNewVal)
- lResult = RegSetValueExString(lKeyValue, Key, 0&, 1&, sNewVal, InLen)
- End If
- lResult = RegFlushKey(lKeyValue)
- lResult = RegCloseKey(lKeyValue)
- End Sub
- Public Function ReadRegistryGetSubkey(ByVal Group As Long, ByVal Section As String, Idx As Long) As String
- Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long, lValueLength As Long, sValue As String, td As Double
- On Error Resume Next
- lResult = RegOpenKey(Group, Section, lKeyValue)
- sValue = Space$(2048)
- lValueLength = Len(sValue)
- lResult = RegEnumKey(lKeyValue, Idx, sValue, lValueLength)
- If (lResult = 0) And (Err.Number = 0) Then
- sValue = Left$(sValue, InStr(sValue, Chr(0)) - 1)
- Else
- sValue = "Not Found"
- End If
- lResult = RegCloseKey(lKeyValue)
- ReadRegistryGetSubkey = sValue
- End Function
- Public Function ReadRegistryGetAll(ByVal Group As Long, ByVal Section As String, Idx As Long) As Variant
- Dim lResult As Long, lKeyValue As Long, lDataTypeValue As Long
- Dim lValueLength As Long, lValueNameLength As Long
- Dim sValueName As String, sValue As String
- Dim td As Double
- On Error Resume Next
- lResult = RegOpenKey(Group, Section, lKeyValue)
- sValue = Space$(2048)
- sValueName = Space$(2048)
- lValueLength = Len(sValue)
- lValueNameLength = Len(sValueName)
- lResult = RegEnumValue(lKeyValue, Idx, sValueName, lValueNameLength, 0&, lDataTypeValue, sValue, lValueLength)
- If (lResult = 0) And (Err.Number = 0) Then
- If lDataTypeValue = REG_DWORD Then
- td = Asc(Mid$(sValue, 1, 1)) + &H100& * Asc(Mid$(sValue, 2, 1)) + &H10000 * Asc(Mid$(sValue, 3, 1)) + &H1000000 * CDbl(Asc(Mid$(sValue, 4, 1)))
- sValue = Format$(td, "000")
- End If
- sValue = Left$(sValue, lValueLength - 1)
- sValueName = Left$(sValueName, lValueNameLength)
- Else
- sValue = "Not Found"
- End If
- lResult = RegCloseKey(lKeyValue)
- ' Return the datatype, value name and value as an array
- ReadRegistryGetAll = Array(lDataTypeValue, sValueName, sValue)
- End Function
- Public Function DeleteSubkey(ByVal Group As Long, ByVal Section As String) As String
- Dim lResult As Long, lKeyValue As Long
- On Error Resume Next
- lResult = RegOpenKeyEx(Group, vbNullChar, 0&, KEY_ALL_ACCESS, lKeyValue)
- lResult = RegDeleteKey(lKeyValue, Section)
- lResult = RegCloseKey(lKeyValue)
- End Function
- ' Example
- ' DeleteValue HKEY_CURRENT_USER, "Software\My Name\My App", "NewSubKey"
- '
- Public Function DeleteValue(ByVal Group As Long, ByVal Section As String, ByVal Key As String) As String
- Dim lResult As Long, lKeyValue As Long
- On Error Resume Next
- lResult = RegOpenKey(Group, Section, lKeyValue)
- lResult = RegDeleteValue(lKeyValue, Key)
- lResult = RegCloseKey(lKeyValue)
- End Function
复制代码
|
|
|
|
|
|
|
|

楼主 |
发表于 22-5-2006 10:12 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 22-5-2006 10:34 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 23-5-2006 09:56 AM
|
显示全部楼层
原帖由 goatstudio 于 22-5-2006 10:34 PM 发表
是 VB.NET 啊...
那么看这里: Working with Windows Registry using VB.NET - The Code Project - VB.NET
谢谢你啊............ |
|
|
|
|
|
|
| |
本周最热论坛帖子
|