tags:

views:

111

answers:

5

After deleting a registry key, I try to re-create it, in C#.

I sometimes get the error:

"Illegal operation attempted on a registry key that has been marked for deletion."

I tried putting in a delay before re-creating it, but didn't help.

Any tips?

A: 

Did you try calling Close()? Failing that, you could try Flush().

Philip Wallace
A: 

As described on TechNet, this happens when something still has an open handle to the key. After all handles are closed, the key will be deleted and can be recreated.

DocMax
+2  A: 

There is still an open handle to the key, so even though it has been marked for deletion, it has not yet been purged. So, you can close any handles that you have which reference the key, or better yet, just modify it instead of deleting it first.

Ed Swangren
+1  A: 

You must Close() all references to the key before you can re-create it.

Serge - appTranslator
A: 

Here's the key phrase:

marked for deletion.

It hasn't been deleted yet! Obviously you can't re-create it, but it would conflict with the (still) existing key. So it's not a problem with you closing or flushing any C# objects: you need to convince the operating system to flush out the registry itself. The only way I know to do that with any certainty is to restart the computer.

Now it might be it's your own program that's preventing the OS from finishing the delete. But then again it might be something else.

Joel Coehoorn
This is not correct (but the last sentence gets close). The OP simply has an open handle to the key, so it cannot be destroyed yet.
Ed Swangren
From the MSDN page for Close():"Closes the key and flushes it to disk if its contents have been modified."
Philip Wallace