programmatically merge .reg file into win32 registry

后端 未结 5 2116
猫巷女王i
猫巷女王i 2021-01-18 21:36

What\'s the best way to programmatically merge a .reg file into the registry? This is for unit testing; the .reg file is a test artifact which will be added then removed at

相关标签:
5条回答
  • 2021-01-18 21:45

    I looked into it by checking out my file associations.

    It seems that a .reg file is just called as the first parameter to the regedit.exe executable on Windows.

    So you can just say regedit.exe "mytest.reg". What I'm not sure of is how to get rid of the dialog box that pops up that asks for your confirmation.

    0 讨论(0)
  • 2021-01-18 21:48

    One of the most frustrating things about writing unit tests is dealing with dependencies. One of the greatest things about Test-Driven Development is that it produces code that is decoupled from its dependencies. Cool, huh?

    When I find myself asking questions like this one, I look for ways to decouple the code I'm writing from the dependency. Separate out the reading of the registry from the complexity that you'd like to test.

    0 讨论(0)
  • 2021-01-18 21:53

    If you're shelling out, I'd use the reg command (details below). If you can tell us what language you're working with, we could provide language specific code.

    C:>reg /?

    REG Operation [Parameter List]

    Operation [ QUERY | ADD | DELETE | COPY | SAVE | LOAD | UNLOAD | RESTORE | COMPARE | EXPORT | IMPORT | FLAGS ]

    Return Code: (Except for REG COMPARE)

    0 - Successful 1 - Failed

    For help on a specific operation type:

    REG ADD /? REG DELETE /? [snipped]

    0 讨论(0)
  • 2021-01-18 21:53

    Use the Win32 API function ShellExecute() or ShellExecuteEx(). If the comment is 'open' it should merge the .reg file. I haven't tested it, but it should work.

    0 讨论(0)
  • 2021-01-18 22:10

    It is possible to remove registry keys using a .reg file, although I'm not sure how well it's documented. Here's how:

    REGEDIT4
    
    [-HKEY_CURRENT_USER\Software\<otherpath>]
    

    The - in front of the key name tells Regedit that you want to remove the key.

    To run this silently, type:

    regedit /s "myfile.reg"
    
    0 讨论(0)
提交回复
热议问题