Solved

Export/Import registry keys

Posted on 2001-06-14
6
243 Views
Last Modified: 2012-06-27
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
Comment
Question by:tvtech
  • 2
  • 2
  • 2
6 Comments
 
LVL 3

Expert Comment

by:RodStephens
ID: 6190918
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
 
LVL 43

Expert Comment

by:TimCottee
ID: 6190984
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
 

Author Comment

by:tvtech
ID: 6194914
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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 3

Expert Comment

by:RodStephens
ID: 6195241
> 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
 
LVL 43

Accepted Solution

by:
TimCottee earned 250 total points
ID: 6195254
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
 

Author Comment

by:tvtech
ID: 7029352
A belated thank you. :)

Regards,
Tony
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error when passing an argument to a vb6 that contains  '/' 7 38
Spell Check in VB6 13 114
Problem to With line 4 57
VBA - If Bookmark = "XXBOOKMARKXX" then 15 29
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question