0

I am trying to delete the registry Folder using C# code but it throws an exception I.e. key doesn't exists...!!! but if key not exists the how then registry object should be not null But in this case it dnt show it as null it contained some value this my code

bool IsDeleted = false;
            try
            {
                regkey = regkey.Replace(@"HKEY_LOCAL_MACHINE\", "");
                using (RegistryKey key = Registry.LocalMachine.OpenSubKey(regkey, true))
                {
                    if (key != null)//here it show that key is exists
                    {
                        IsDeleted = DeleteKey(key, regkey);//in this line It generate exception

                    }
                }
            }
            catch
            {
            }
             return IsDeleted;

this is Delete Code

 public static bool DeleteKey(RegistryKey hKey, string strPath)
        {
            bool flag1 = false;
            int num1 = 0;
            int num2 = 0;
            try
            {
                num2 = 1;
                hKey.DeleteSubKeyTree(strPath);
                flag1 = true;
            }
            catch (Exception obj1) //when (?)
            {
                Exception exception2 = (Exception)obj1;
                Exception exception1 = exception2;
                if (num1 == 0)
                {
                    num1 = -1;
                    switch (num2)
                    {
                        case 1:
                            {
                                goto Label_0058;
                            }
                    }
                    throw;
                }
            }
        Label_0058:
            if (num1 != 0)
            {
            }
            return flag1;
        }

please help me for this thnx

1 Answers1

0

this occur due to the permissions issue in windows 7

try
            {
                new System.Security.Permissions.RegistryPermission(System.Security.Permissions.PermissionState.Unrestricted).Assert();
                hKey.DeleteSubKeyTree(strPath);
                flag1 = true;
            }
            catch (Exception obj1) //when (?)
            {
            }
            finally
            {
                System.Security.Permissions.RegistryPermission.RevertAssert();
            } 

You can embed this manifest into your application.

<?xml version="1.0" encoding="utf-8" ?> 
<asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <assemblyIdentity version="1.0.0.0" name="MyApplication" />
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
            </requestedPrivileges>
        </security>
    </trustInfo>
</asmv1:assembly>
Tanmay Nehete
  • 2,138
  • 4
  • 31
  • 42