Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 358737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:19:40+00:00 2026-05-12T12:19:40+00:00

I have a piece of code like this try { RegistryKey regKey = Registry.LocalMachine.CreateSubKey(SOFTWARE\\xxxx\\yyyyy);

  • 0

I have a piece of code like this

try
{
    RegistryKey regKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\xxxx\\yyyyy");
    // more code
}
catch
{
}

I don’t like the use of the empty catch block. But, it’s useful because if the user don’t have permissions to access the Registry, nothing should be done.

This piece of code gets called many times and, apart of a bad practice, I think it has poor performance.

I’ve been looking for a way to check the registry permissions before trying to access it, but the only way I’ve found to do it it’s checking for a exception with


RegistryPermission.Demand()

and check for a exception. So, that gives me no advantage with the initial approach.

¿Is there a way to check for the Registry access permissions without artificially throwing or having to check for exceptions?

Edit:

Well, looks like the .NET preferred way of doing this is trying to access the resource and check for exceptions. The article Yannick pointed shows how complex it is to deal with the Windows security model, looking for the desired access manually.
So, what I’m going to do is redesign this code a little so it only checks once for the access (catching the exception) and keeps that information, instead of constantly throwing exceptions.
This has the drawback that if the user changes the security settings “on the fly”, the code will keep denying access to the Registry. However, this is preferred if there isn’t a simple and clean way of checking for access.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T12:19:40+00:00Added an answer on May 12, 2026 at 12:19 pm

    Since you are creating a new key, shouldn’t you just check the parent’s permissions once?

    Edit: I am unsure if there are managed ways, but you could try CheckAccess() in Stdprov.dll: http://msdn.microsoft.com/en-us/library/aa384911%28VS.85%29.aspx

    Edit2: Have you tried http://msdn.microsoft.com/en-us/library/1w66447a.aspx ?

    Edit3:

    26) Access checks in .NET

    In part 2, we went through performing
    access checks using the Win32
    AccessCheck API. Unfortunately, there
    doesn’t seem to be an equivalent
    managed function that can perform the
    task. It’s not recommended for you
    perform an access check in .NET.
    Instead, you should make use of
    role-based security to perform an
    access check for you (This is what
    ReadSD does. Before ReadSD writes a
    security descriptor, it needs to check
    if you are allowed to alter the
    security descriptor. It does this by
    reading the security descriptor and
    calling GenericPrincipal.IsInRole to
    check for group membership). This only
    works if your objects are designed for
    role-based security. It does not work
    with objects secured by security
    descriptors.

    If you need to perform an access check
    on an object with a security
    descriptor (Registry key in our case),
    you wouldn’t use AccessCheck to do so
    (even in Win32). The proper method is
    to open up the registry key, and if
    the security descriptor denies access,
    you will get an “access denied”
    exception.

    In simple access checks, you may be
    able to perform the access check
    yourself with the help of an
    imperative role-based security (fig.
    38):

    http://www.codeproject.com/KB/system/accessctrl3.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a piece of code which does something like this. try {
I have a piece of code like this: <div id=container runat=server> <div id=parent runat=server>
I have a piece of code like this: Class Program { static StreamReader sr
I have a piece of code like this: HashSet<Object> Set = new HashSet<Object>(); //add
I have a DSL - like piece of Ruby code that looks like this:
Let's say you have a piece of code like: resource = allocateResource(); try {
I have this piece of code that basically checks if one or more files
I have a piece of code that goes like this File file = null;
The piece of code that I have looks some what like this: glbl_array =
I have a piece of code like the following: try { Work: while(true) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.