Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 292
  • Last Modified:

Export/Import registry keys

Hi,

Does anyone have some code that will export all keys and data in a known registry branch to file ie

HKEY_CURRENT_USER\Software\VB and VBA Program Settings\Auto-BackupFTP

as well as recreate all Keys when importing again please.

Thank you.

Regards,
    Tony
0
tvtech
Asked:
tvtech
  • 2
  • 2
  • 2
1 Solution
 
RodStephensCommented:
If you don't need to do this with a VB program, the Registry Editor will do it.

Use Start/Run/regedit to start it. Select the branch you want to export. From the File menu, select Export Registry File. in the file selection dialog, make sure the Select Branch option is selected and that the right branch is filled in.

The Registry Editor will import a saved branch similarly through the file menu, or just double click on the .reg file.
0
 
TimCotteeCommented:
Private Sub ReadSettings()
    Open "c:\testbed\Auto-BackupFTP.reg" For Input As #1
    Do
        Line Input #1, strLine
        arySettings = Split(strLine, "=")
        SaveSetting "Auto-BackupFTP ", "Auto-BackupFTP ", arySettings(0), arySettings(1)
    Loop Until EOF(1)
    Close #1
End Sub

Private Sub WriteSettings()
    Dim aryRegistry()
    Dim varRegistry As Variant
    varRegistry = GetAllSettings("Auto-BackupFTP ", "Auto-BackupFTP ")
'    MsgBox UBound(varRegistry)
    Open "c:\testbed\Auto-BackupFTP.reg" For Output As #1
    For intKey = LBound(varRegistry) To UBound(varRegistry)
        Print #1, varRegistry(intKey, 0) & "=" & varRegistry(intKey, 1)
    Next
    Close #1
End Sub

Should get you started, though it only does it for a specific section at a time.
0
 
tvtechAuthor Commented:
Thanks TimCottee,

Your answer is on the right track, but looking at my Post, I realised that I had missed the fact that there are many sub Keys (Child nodes) within the Parent key and the code is not recursive. Any ideas?

Thanks again.

Regards,
   Tony

PS - Thanks Rod, but Regedit is not an option. Code is the only path I can take.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
RodStephensCommented:
> PS - Thanks Rod, but Regedit is not an option. Code is the only path I can take.

Gotta try the easy solutioun first ;-)

Take a look at this example. It doesn't save the information into a file, but it recursively traverses it. You can write it into a file. Use regedit to export a piece of registry to see what the format should look like.

To load the file, I think you can use AppExecute to "open" it, though that will probably make a dialog appear. Or you can dig through the file yourself creating the keys.

Nto a complete solution, but it should get you started.
0
 
TimCotteeCommented:
tvtech,

Here is a sample which should achieve your aim, basically it uses the API functions which allow registry access to enumerate the keys and to save the results to a file

Private Const HKEY_CURRENT_CONFIG = &H80000005
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_CURRENT_USER = &H80000001
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
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 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 Byte, lpcbData As Long) As Long

Private Sub Command1_Click()
    WriteSettings "c:\testbed\AllSettings.reg", HKEY_CURRENT_USER, "Software\VB And VBA Program Settings\Auto-BackupFTP ", List1
'Specify the file name, the root key that you want to use, and an optional list box on the form to display the results of the enumeration.
End Sub

Private Function StripTerminator(sInput As String) As String
    Dim ZeroPos As Integer
    'Search the first chr$(0)
    ZeroPos = InStr(1, sInput, vbNullChar)
    If ZeroPos > 0 Then
        StripTerminator = Left$(sInput, ZeroPos - 1)
    Else
        StripTerminator = sInput
    End If
End Function

Private Sub EnumKeys(ByVal FileName As String, ByVal HiveName As Long, ByVal KeyName As String, Optional ByVal Level As Integer = 0, Optional ListDisplay As ListBox)
    Dim hKey As Long, Cnt As Long, sSave As String
    RegOpenKey HiveName, KeyName, hKey
    EnumValues FileName, HiveName, KeyName, Level + 1, ListDisplay
    Do
        sSave = String(255, 0)
        If RegEnumKeyEx(hKey, Cnt, sSave, 255, 0, vbNullString, ByVal 0&, ByVal 0&) <> 0 Then Exit Do
        If Not (ListDisplay Is Nothing) Then ListDisplay.AddItem String(Level, vbTab) & StripTerminator(sSave)
        EnumKeys FileName, HiveName, KeyName & "\" & StripTerminator(sSave), Level + 1, ListDisplay
        Cnt = Cnt + 1
    Loop
    RegCloseKey hKey
End Sub

Private Sub EnumValues(ByVal FileName As String, ByVal HiveName As Long, ByVal KeyName As String, Optional ByVal Level As Integer = 0, Optional ListDisplay As ListBox)
    Dim hKey As Long, Cnt As Long, sSave As String
    Dim AppName As String, Section As String, Key As String, Value As Variant
    RegOpenKey HiveName, KeyName, hKey
    Do
        sSave = String(255, 0)
        If RegEnumValue(hKey, Cnt, sSave, 255, 0, ByVal 0&, ByVal 0&, ByVal 0&) <> 0 Then Exit Do
        intPos1 = InStr(Replace(KeyName, "Software\VB And VBA Program Settings", "") & "\", "\")
        intPos2 = InStr(intPos1 + 1, Replace(KeyName, "Software\VB And VBA Program Settings", "") & "\", "\")
        AppName = Mid(Replace(KeyName, "Software\VB And VBA Program Settings", ""), intPos1 + 1, intPos2 - intPos1 - 1)
        intPos1 = intPos2 + 1
        intPos2 = Len(KeyName) 'InStr(intPos1, Replace(KeyName, "Software\VB And VBA Program Settings", "") & "\", "\")
        Section = Mid(Replace(KeyName, "Software\VB And VBA Program Settings", ""), intPos1, intPos2 - intPos1)
        varValue = GetSetting(AppName, Section, StripTerminator(sSave), "N/A")
        If Not (ListDisplay Is Nothing) Then ListDisplay.AddItem String(Level, vbTab) & "* " & StripTerminator(sSave) & "=" & varValue
        If FileName <> "" Then
            Open "c:\testbed\Settings.reg" For Append As #1
            Print #1, AppName & "," & Section & "," & StripTerminator(sSave) & "," & varValue
            Close #1
        End If
        Cnt = Cnt + 1
    Loop
    RegCloseKey hKey
End Sub

Private Sub WriteSettings(FileName As String, HiveName As Long, KeyName As String, Optional ListDisplay As ListBox)
    If Not (ListDisplay Is Nothing) Then ListDisplay.Clear
    On Error Resume Next
    'Remove the file if it exists, this does not handle any error if the file is open elsewhere!
    Kill FileName
    EnumKeys FileName, HiveName, KeyName, 0, ListDisplay
End Sub

Private Sub ReadSettings(FileName As String)
    Dim AppName As String, Section As String, Key As String, Value As Variant
    Open FileName For Input As #1
    Do
        Input #1, AppName, Section, Key, Value
        SaveSetting AppName, Section, Key, Value
    Loop Until EOF(1)
    Close #1
End Sub
0
 
tvtechAuthor Commented:
A belated thank you. :)

Regards,
Tony
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now