I am trying to access Software\Microsoft\Windows\CurrentVersion\Group Policy Objects{AEE4EB78-4042-42EC-9C84-D08BA6C3047B}Machine\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate to read update value PauseFeatureUpdatesStartTime.
Trying to access entire path was failing, so I started trying it iece by piece.
key = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion"); worked fine but
key = Registry.CurrentUser.OpenSubKey(#"Software\Microsoft\Windows\CurrentVersion\Group Policy Objects"); fails with key equal to NULL?!?
I believe the key path is correct because I am displaying it in regedit and
regedit is displaying "Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects{AEE4EB78-4042-42EC-9C84-D08BA6C3047B}Machine\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate" as the path.
NOTE!!! these keys\values may not exist if you have not "activated" then with gpedit.msc. In gpedit, look for "Computer Configuration\Administrative Templates\Windows Components\Windows Update\Windows Update For Business". You have to "activate" the first two items.
Any thoughts?
thanks,
Related
I want add item to a key in registry and find "CreateSubKey" command but point is this command create sub key not item to a key. for example path of "Computer\HKEY_USERS.DEFAULT\Control Panel\International" has about 40 item and 3 sub key. I want add item to "\International" not sub key.
another problem is that when I want add subkey to another users in "Computer\HKEY_USERS" path ,I got error "cant access other user registry" .how could I handle that with code without login as admin in windows?
LOOK AT PICTURE: enter image description here
best regards.
Create the key you are interested in and then set the values:
RegistryKey rk = Registry.Users.CreateSubKey("DEFAULT\\Control Panel\\International");
rk.SetValue("a number", 42);
rk.SetValue("my string", "fiftythree");
You have to be elevated administrator to modify keys in roots other than CurrentUser. Normal users are not allowed to modify machine settings nor the settings of other users.
To begin, I recognize that this question appears similar to others. However, my research thus far has not found a solution to the specific problem I am facing, just a lot of solutions to problems with similar circumstance.
I am new to registry functions, so I've been using a VM to mess around with them and see what I can do. Currently, I am trying to test the creation, reading, and subsequent deletion of a subKey and HKEY_CURRENT_USER. At the moment, I can do everything in that list except deletion. The relevant code is as follows:
//This first sample will attempt to create a test key under HKEY_CURRENT_USER
Console.WriteLine("Creating subkey under HKEY_CURRENT_USER");
RegistryKey testKey2 = Registry.CurrentUser.CreateSubKey("SubKeyTester");
Console.WriteLine("testKey2 is now assigned to {0}", testKey2);
//This ensures that testKey2 is the value that I think it is
Console.WriteLine("testKey2 value = {0}\n", testKey2);
with an output of:
Beginning test...
Creating subkey under HKEY_CURRENT_USER
testKey2 is now assigned to HKEY_CURRENT_USER\SubKeyTester
testKey2 value = HKEY_CURRENT_USER\SubKeyTester
Notably, testKey2 has stored "HKEY_CURRENT_USER\SubKeyTester" rather than the "SubKeyTester" that I expected.
After this, I'm able to check the subkeys under HKEY_CURRENT_USER and verify that yes, "SubKeyTester" is indeed present among the CurrentUser subkeys. Now, I just need to delete it. My code is as follows:
//This portion of the test will attempt to delete SubKeyTester from
//HKEY_CURRENT_USER
Console.WriteLine("Attempting to delete test subkey\n");
try
{
Registry.CurrentUser.DeleteSubKeyTree(testKey2.ToString());
Console.WriteLine("Target has been deleted\n");
}
catch(Exception e)
{
Console.WriteLine("The key targeted for deletion... is not found.\nError: {0}\n", e);
}
//Another safety check to verify that only SubKeyTester has been deleted
Console.WriteLine("There are {0} subkeys under {1}.",
Registry.CurrentUser.SubKeyCount.ToString(), Registry.CurrentUser.Name);
foreach (string subKeyName in Registry.CurrentUser.GetSubKeyNames())
Console.WriteLine(subKeyName);
testKey2.Close();
The output informs me:
"Error: System.ArgumentException: Cannot delete a subkey tree because the subkey does not exist."
It then lists all the subkeys under HKEY_CURRENT_USER, which still includes the testKey "SubKeyTester".
I believe the problem could be solved by just hard-coding the path to that subkey in the DeleteSubKeyTree call, but I want to avoid that. I'd rather just be able to invoke testKey2 as a parameter and delete the key that way. Is there a way to do that?
I have found my error, and I was on the right track. The correct code is as follows:
Console.WriteLine("Creating subkey under HKEY__CURRENT_USER\n");
const string testKey2 = "SubKeyTester";
Registry.CurrentUser.CreateSubKey(testKey2);
This way, testKey2 is always "SubKeyTester". Then, the only other alteration needed is to the delete function's parameter.
Registry.CurrentUser.DeleteSubKeyTree(testKey2);
removing the ToString() method.
I believe the problem was as I said, that testKey2 was getting "HKEY_CURRENT_USER//SubKeyTester" instead of just "SubKeyTester". But this way, testKey2 only gets "SubKeyTester", which allows for a correct filepath to the appropriate key.
I had tried using
Registry.DeleteSubKeyTree(testKey2.ToString());
to get around the pathing error, but "Registry" does not have a DeleteSubKeyTree method, so that simply didn't work.
Also, this solution does not require a .close() statement, because no key was ever opened.
Not a solution, but I found the error message is a red herring.
One of the subkeys had permissions preventing it from being deleted.
Confirmed in Registry Editor too:
The following path always returns false:
Directory.Exists(#"\\SERVERIP\aFolder\bFolder");
// where SERVERIP is the server-IP which is being accessed using Impersonation
After debugging the code, it places double-slashes in the Debugger.
I have accessed the above file path without the # and double-quotes in WindowsExplorer.
What am I doing wrong?
[ The code will run on a network ]
The problem might be in the paths-[Source/Destinations] (both or one of it[source/destination] might be causing the problem) due to the default-paths used by Visual-Studio. So let me explain how to check wether the paths are correct/incorrect step by step.
Configuring ** SOURCE-PATH **:
Some times this path DRIVE:\ProgramFiles\IISExpress (or some other path depending on the installation location of IIS) gets concatenated with the SOURCE-PATH you give in the input To solve this problem, follow/verify these steps:
Ensure that the SOURCE-PATH or File you are using is in the Project-Folder
To Access the SOURCE-PATH or File. Always use this path/way:
// 1. SOURCE-PATH + fileName with Extension<br>
Server.MapPath("~\FolderInsideProjectFolder\", "fileName.extension");
Configuring ** DESTINATION-PATH (to a Mapped-NETWORK) **:
This path creates a problem if the path you entered has some words mispelled OR if you don't have access to the specified Server-IP[DestinationServerIP]. To solve this problem, follow/verify these steps:
Before Accessing the DESTINATION-PATH or File , ensure that the IP-Address you are referring to is Accessible to the Account under which your Application-code is running.To learn how to run Applications under an Account. See Impersonization
To Access the DESTINATION-PATH or File. Always use this path/way:
// 2. DESTINATION-PATH + fileName with Extension
#"\\SERVERIP\aFolder\bFolder" + "fileName.extension";
NOTE:
Remember that the SOURCE-PATH can be checked if it (exists/does not exist) by addressing its Fully-Qualified-Address and in that case, it will return true if it exists (The full-path that windows-explorer shows you in the Address Bar (Windows-Explorer) like DRIVE:/....../
EXTRA-INFORMATION: (as it was the basic INTENSION)
One line instruction to Copy the file from local-system → networked-mapped drive/path is:
System.IO.File.Copy(
Server.MapPath("~\FolderInsideProjectFolder\", "fileName.extension"),
#"\\SERVERIP\aFolder\bFolder" + "fileName.extension"
[, true ] // Optional if you want the file to be over-written or not
);
Please inform, if any thing still is not cleared (but after some nice searching ☋ ☛ )
Many a times I have seen file (or directory) access problems when the user (a human, system user such as IIS_IUSR or an application) lacks required privileges.
According to this question where the asker is facing similar problem, I believe that this may help you.
Let us know, if it helps.
I'm getting reported errors from users who are receiving the error "System.Security.SecurityException, Requested registry access is not allowed." when trying to read the registry. I can't think why someone would not have permission to read the registry and I'm unable to reproduce the problem on my Windows 7 PC. Affected users are running .NET 4.0
Here's the C# code I'm using:
var baseReg = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
var key = baseReg.OpenSubKey(RegKey, RegistryKeyPermissionCheck.ReadSubTree);
if (key != null)
{
var value = key.GetValue("DisableAutoUpdate", 0);
if (value != null)
{
updatesDisabled = Convert.ToBoolean(value);
}
}
EDIT:
I've checked permissions on the registry key concerned for the affected user and standard Users have read permission for that key.
EDIT 2 and SOLUTION:
According to the affected user, installing .NET 4.5.2 resolves the problem! I'm not sure why.
Thanks to #nozzleman's answer below this is fixed by forcing it to open the key as read-only. However it's odd that .NET 4.0 as 4.5.2 appear to behave differently.
There is an overload of the OpenSubKey(..)-Method that allows to add a third parameter. You could try passing RegistryRights.ReadKey with that one and see if that solves the issue.
baseReg.OpenSubKey(
RegKey,
RegistryKeyPermissionCheck.ReadSubTree
RegistryRights.ReadKey);
Alternatively, try the other overload accepting 2 parameters like so
baseReg.OpenSubKey(RegKey, false);
This leads to opening the subkey readonly, and you dont neet to read the whole sub tree in the given szenario..
what i am trying to do is to have multiple
user names and folder paths in one key.
so i have this structure
HKEY_LOCAL_MACHINE
-- SOFTWARE
-- XYZ
-- userDB
now in userDB i have the info like this
> NAME TYpe Data
>
> Admin Reg_sz C:\Desktop
>
> Admin2 REG_SZ C:\xyz\logs
how can i read the values in userDB...
any suggestions.. thanks
i tried this code:
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\xyz");
if (masterKey == null)
{
//Console.WriteLine("Null Masterkey!");
}
else
{
table.Rows.Add(false, masterKey.GetValue("userDB"), DateTime.Now);
dataGridView2.DataSource = table;
//Console.WriteLine("MyKey = {0}", masterKey.GetValue("userDB"));
}
masterKey.Close();
but i get the error
Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\xyz' is denied.
While you talk about reading values in your topic, your code actually writes to the registry.
You can't write to most of the HKLM part of the registry by default as a limited user. A limited users may not destroy/manipulate these keys because that's a security risk.
You could have your setup program(running with admin privs) change the permissions for your shared registry key. But that's bad style, and I wouldn't do it.
When using asp.net there might be additional restrictions related to the medium trust model.
You have:
RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\xyz");
RegistryKey.CreateSubKey is documented as:
Creates a new subkey or opens an existing subkey for write access.
Opening for write access most likely requires write privileges.
RegistryKey.OpenSubKey is used to open a key for read access. So it most likely requires no writing privileges.
At which point are you getting access denied? Are you running this code elevated or as administrator?
Chances are you are failing when calling CreateSubKey(), which when writing to HKEY_LOCAL_MACHINE requires elevated permissions.
I think Registry.LocalMachine.CreateSubKey("SOFTWARE\xyz") would try to open the key with write access if that key exists.
Try to open the key with read access instead.
I think that you can use CreateSubKey(String, RegistryKeyPermissionCheck) instead to specify permission access.
For more information, please refer to MSDN: http://msdn.microsoft.com/en-us/library/dd411617.aspx