How do I check whether a user is allowed to read / write a particular registry key?

前端 未结 6 590
清歌不尽
清歌不尽 2020-12-31 08:14

Does anybody know how I can programmatically check (using C#) whether my program will be able to read / write a particular registry key (specifically: \"SOFTWARE\\Microsoft\

6条回答
  •  无人及你
    2020-12-31 09:08

    One thing you should know about permissions is that they are volatile. That means you could do your security check on the registry key, attempt to add your value only if the check passes, and then still fail with an insufficient access exception because the permissions changed in between when you made the check and when you acted on the results. This is possible even if they are consecutive statements in your program.

    Granted security permissions tend to be relatively stable, but the chance still exists. This means that you must have code to handle the security exception, and if you have to do that anyway there's not really any point in making the check in the first place. Instead, put your time into making your exception handler a little bit better.

    That said, "boo" to any app that wants to run something at start-up. YAGNI.

提交回复
热议问题