Use c# code to access the registry, can not modify the data to the registry

Joined
Aug 4, 2021
Messages
11
Reaction score
1
What I want to achieve is to hide and show desktop icons by modifying the registry.
Other code will not be written, the main code is as follows:
C#:
try
{
int i = 1;
RegistryKey key = Registry.CurrentUser;
RegistryKey hide = key.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", true);
hide.SetValue("HideIcons",i, RegistryValueKind.DWord);
key.Close();
Process[] MyProcess = Process.GetProcessesByName("explorer");
MyProcess[0].Kill();
}
catch (Exception ex)
{
MessageBox.Show("Error!!! \n" + ex);
}
Now it is possible to modify it, but changing i=1 to i=0 is not possible. The default registry value is 0; that is, you cannot write 0.
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
That's a tricky one. You can also try:
C#:
hide.DeleteValue("HideIcons");
But, that's not working for me, either, for that specific registry (writing zeroes and deleting works for arbitrarily named registries). Explorer seems to re-create it when it's restarted based on information elsewhere. Is there another registry to do accomplish this?
 
Joined
Aug 4, 2021
Messages
11
Reaction score
1
I solved the problem by this method.

Code:
Int32 tempInt = 0; //predefine a signed 32-bit number
//conversion within the unchecked statement block, no overflow check
unchecked
{
tempInt = Convert.ToInt32("fffffffff", 16); //force conversion to a signed 32-bit number
}
//at this point the tempInt is already a signed 32-bit number and can be written directly to the registry
SetValue("HideIcons",tempInt, RegistryValueKind.DWord);
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
Excellent, I'm glad you got it working! Your solution still throws an OverflowException for me, which should be impossible. I'd imagine it's not worth figuring out, though. On my end, the zero is taking hold but Explorer is reloading the one about 200ms after being killed. I used the following loop to watch for it.
C#:
int j = 0;
for (; j < 100; j++)
{
    Thread.Sleep(10);
    Console.WriteLine(hide.GetValue("HideIcons"));
    if (hide.GetValue("HideIcons").ToString() == "1")
    {
        break;
    }
}
Console.WriteLine(j * 10 + " ms");
Again, hardly seems worth understanding if you've got it working. Well done.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top