Cannot get registry key program to work

WARNING: Be careful NOT to run the below program once you or myself debug it unless you REALLY want to delete your Visual Studio registry keys. I put a return 0 statement in my program to avoid damage. In my case, I have to build it on one machine, then run on another. I know there is a compiler for C# probably somewhere on my other machine so perhaps I should have used it. It can be used command-line even on the machine with Visual Studio "uninstalled" (for lack of a better term).

I had been using Visual Studio Enterprise 2015 Update 2. As I added Xamarin and some other tools, I reluctantly started to move to Update 3. Microsoft's installer installed all the new stuff all over my hard-drive - not in the original installation directory which I always choose to be something short - like 3 characters long. So I decided to uninstall Visual Enterprise 2015 - there were actually two of them. After doing that, I realized that their uninstall program does NOT work. So I decided to just install the entire 7.2GB Update 3, but it won't let me change the directory to what I want (it's disable/grayed) - yet another problem with their software failing to do something.

So I came upon this guy who wrote a registry delete key program that will hopefully get rid of some keys and allow me to change the directory. By the way, when I installed the ENTIRE 7.2GB program on my laptop (with Xamarin, Android, etc..), it seemed to go well. So I think I'm going in the right direction. So here's the BIGGEST PROBLEM: I took the guys program, compiled it as a C# program on my laptop, then emailed the code over to my main machine and it runs but I can never set the permissions on enough stuff to allow the program to open the registry. The same old story again with permissions. Does someone know why I can't just set everything as Administrator and get the programs. I even assured the desktop that it's ok that the program came from another machine. Man, this stuff is making me a lose a lot of time. When will it stop? Forget all the permissions crap. It's causing our computers to not be able to run. If someone logs in to their computer as an Administrator, they should never have a problem. Any ideas appreciated. A thought just occured to me that perhaps the error: "Unable to open registry, make sure to run as admin" might just be a bug so I WILL look into that. Perhaps it's not logical to "ASSUME" that it's permissions. I'll try something. In the meantime, any help appreciated as I take a stab at debugging this. I'm wondering, do I need to put in HKEY_LOCAL_MACHINE in place of @. I don't know. It's the HKEY_LOCAL_MACHINE sub-hive that I'm interested in.


using System;
using Microsoft.Win32;

namespace VsCleanup
{
class Program
{
static int Main(string[] args)
{
Console.WriteLine("Enter the old VS path to purge from the registry:");
string OldPath = Console.ReadLine().Trim().ToLowerInvariant();

string RootKeyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Components";
RegistryKey RootKey = Registry.LocalMachine.OpenSubKey(RootKeyName, true);
if (RootKey == null)
{
Console.WriteLine("Unable to open registry, make sure to run as admin");
return 1;
}
var SubkeyNames = RootKey.GetSubKeyNames();
foreach (var SubkeyName in SubkeyNames)
{
var Subkey = RootKey.OpenSubKey(SubkeyName, true);
var ValueNames = Subkey.GetValueNames();
bool Delete = false;
foreach (var ValueName in ValueNames)
{
if (Subkey.GetValueKind(ValueName) == RegistryValueKind.String)
{
var Value = Subkey.GetValue(ValueName) as string;
if (Value.ToLowerInvariant().StartsWith(OldPath))
{
Console.WriteLine("{0}: {1}", SubkeyName, Value);
Delete = true;
break;
}
}
}
Subkey.Close();
if (Delete)
RootKey.DeleteSubKeyTree(SubkeyName, true);
}
RootKey.Close();
return 0;
}
}
}
Last edited on
The call to Registry.LocalMachine.OpenSubKey is returning NULL, on both the Desktop machine and the laptop where I'm building the app.

Now there is yet another problem. If I take the Platform target flag "Prefer 32-bit" off since I want 64-bit, I get an exception thrown.
Last edited on
I wonder if I need to use a 64-bit API:

As in:

RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
I changed the lines to:

RegistryKey hklm = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
RegistryKey RootKey = hklm.OpenSubKey(RootKeyName);

And now a call to Console.WriteLine(RootKey) prints out the proper key so I'll email a new application and try it out.

Should work!?
So now I make it all the way to the line you see above:

if(Delete)
RootKey.DeleteSubKeyTree(SubkeyName, true);

This throws an exception: ThrowUnAuthorizedAccessException
I am now testing to see if the parent of a subtree has to be writeable. I am changing the original OpenSubKey to have true as the 2nd parameter. That's all I can think of right now.
That solved the problem.

In the above where I have the line:
RegistryKey RootKey = hklm.OpenSubKey(RootKeyName);

It needs to be changed to:
RegistryKey RootKey = hklm.OpenSubKey(RootKeyName, true);

All of the subkeys were just deleted - I would guess a hundred or more.

Now I can see if when I run the Visual Studio installer (7.2GB Update 3 version), that it allows me to set the directory to install to.
Good! I can NOW browse for a folder to install to! Of course, it still prompts me with the old stupid one: C:\Program Files (x86)\Microsoft Visual Studio 14.0 - that piece of trash.
Topic archived. No new replies allowed.