jm.
jm.

Reputation: 23714

Error re-creating registry key that I just deleted

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?

Upvotes: 0

Views: 1443

Answers (5)

Philip Wallace
Philip Wallace

Reputation: 8015

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

Upvotes: 0

Joel Coehoorn
Joel Coehoorn

Reputation: 415600

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.

Upvotes: -1

Serge Wautier
Serge Wautier

Reputation: 21878

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

Upvotes: 1

Ed Swangren
Ed Swangren

Reputation: 124632

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.

Upvotes: 2

DocMax
DocMax

Reputation: 12164

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.

Upvotes: 0

Related Questions