<p>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 the start and end of testing.</p>

<p>Or, if there's a better way to unit test against the registry...</p>

[{'answer_id': 35092, 'author': 'Javache', 'author_id': 1074, 'author_profile': 'https://Stackoverflow.com/users/1074', 'pm_score': 2, 'selected': False, 'text': '<p>I looked into it by checking out my file associations.</p>\n\n<p>It seems that a .reg file is just called as the first parameter to the <code>regedit.exe</code> executable on Windows. </p>\n\n<p>So you can just say <code>regedit.exe "mytest.reg"</code>. What I\'m not sure of is how to get rid of the dialog box that pops up that asks for your confirmation.</p>\n'}, {'answer_id': 35098, 'author': 'jmatthias', 'author_id': 2768, 'author_profile': 'https://Stackoverflow.com/users/2768', 'pm_score': 1, 'selected': False, 'text': "<p>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.</p>\n"}, {'answer_id': 35162, 'author': 'Matt Dillard', 'author_id': 863, 'author_profile': 'https://Stackoverflow.com/users/863', 'pm_score': 4, 'selected': True, 'text': '<p>It is possible to remove registry keys using a .reg file, although I\'m not sure how well it\'s documented.  Here\'s how:</p>\n\n<pre><code>REGEDIT4\n\n[-HKEY_CURRENT_USER\\Software\\&lt;otherpath&gt;]\n</code></pre>\n\n<p>The <code>-</code> in front of the key name tells <code>Regedit</code> that you want to remove the key.</p>\n\n<p>To run this silently, type:</p>\n\n<pre><code>regedit /s "myfile.reg"\n</code></pre>\n'}, {'answer_id': 53014, 'author': 'Jay Bazuzi', 'author_id': 5314, 'author_profile': 'https://Stackoverflow.com/users/5314', 'pm_score': 0, 'selected': False, 'text': "<p>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?</p>\n\n<p>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.</p>\n"}, {'answer_id': 53017, 'author': 'Jon Galloway', 'author_id': 5, 'author_profile': 'https://Stackoverflow.com/users/5', 'pm_score': 3, 'selected': False, 'text': "<p>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.</p>\n\n<p>C:>reg /?</p>\n\n<p>REG Operation [Parameter List]</p>\n\n<p>Operation  [ QUERY   | ADD    | DELETE  | COPY    |\n               SAVE    | LOAD   | UNLOAD  | RESTORE |\n               COMPARE | EXPORT | IMPORT  | FLAGS ]</p>\n\n<p>Return Code: (Except for REG COMPARE)</p>\n\n<p>0 - Successful\n  1 - Failed</p>\n\n<p>For help on a specific operation type:</p>\n\n<p>REG ADD /?\n  REG DELETE /?\n[snipped]</p>\n"}]
['https://Stackoverflow.com/questions/35070', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1683/']