After deleting a registry key, I try to re-create it, in C#.
I sometimes get the erro开发者_JAVA百科r:
"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?
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.
You must Close()
all references to the key before you can re-create it.
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.
Did you try calling Close()? Failing that, you could try Flush().
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.
精彩评论