3

I am messing about with my Windows Phone after finding and using the registry edit to allow it to be accessed through explorer. My aim is to write a quick console application to perform this registry edit on any machine it is plugged into.

THIS IS THE WINDOWS 7 REGISTRY. NOT THE PHONE Here are the steps I am trying to follow

So far I have written code that will find all instances of device registry entries containing 'ZuneDriver'

RegistryKey start = Registry.LocalMachine;
    using (RegistryKey root = start.OpenSubKey(@"SYSTEM\CurrentControlSet\Enum\USB"))
    {
        string myKey = "ZuneDriver";
        SearchSubKeys(root, myKey);

The issue here is that, the registry key 'ZuneDriver' is a subkey of the 'Device Parameters' key I will need to change a value of.

At the moment, the results of the search are stored in:

    static System.Collections.Specialized.StringCollection log = new System.Collections.Specialized.StringCollection();

by my search function:

public static void SearchSubKeys(RegistryKey root, String searchKey)
{
    foreach (string keyname in root.GetSubKeyNames())
    {
        try
        {
            using (RegistryKey key = root.OpenSubKey(keyname))
            {
                if (keyname == searchKey)
                {
                    log.Add(key.Name);
                }


                SearchSubKeys(key, searchKey);
            }
        }
        catch (System.Security.SecurityException)
        {
        }
    }
}

A stored result looks like this:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_045E&PID_04EC&MI_00\7&b85dba6&0&0000\Device Parameters\ZuneDriver

I would like to get my program to have registry access to:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_045E&PID_04EC&MI_00\7&b85dba6&0&0000\Device Parameters

to edit the value of a DWORD called 'ShowInShell'

but it needs to have determined \VID_045E&PID_04EC&MI_00\7&b85dba6&0&0000\ through the initial search.....

Any pointers or suggestions much appreciated :)

Cheers,

Ben

  • That has to be a rabbit hole. You surely can't get write access to that key. Even if you did, it still wouldn't work since the driver has no clue that one of its parameters just got hacked without a phone reboot. – Hans Passant Feb 19 '12 at 11:46
  • Edited the intial post. This is the windows registry, not the phone. Is a simple thing to do using regedit. Am just trying to create code to automate the process :) –  Feb 19 '12 at 11:50
  • possible duplicate of [How to force C# App to run as administrator on Windows 7](http://stackoverflow.com/questions/2818179/how-to-force-c-sharp-app-to-run-as-administrator-on-windows-7) – Hans Passant Feb 19 '12 at 12:02
  • I am not worried about access right now, I am purely asking for direction on how once I have found the registry key 'ZuneDriver', I can open 'Device Parameters' above it :) –  Feb 19 '12 at 12:05
  • But thank-you for the link provided, it has helped with something else I was working on! –  Feb 19 '12 at 12:06

1 Answers1

0

I would do it like this:

public static void SearchSubKeys(RegistryKey root, String searchKey)
{
    bool containsKey = false;
    foreach (string keyname in root.GetSubKeyNames())
    {
        try
        {
            using (RegistryKey key = root.OpenSubKey(keyname))
            {
                if (keyname == searchKey)
                {
                    containsKey = true;
                }

                SearchSubKeys(key, searchKey);
            }
        }
        catch (System.Security.SecurityException)
        {
        }
    }
    if(containsKey){
        using (RegistryKey key = root.CreateSubKey("Device Parameters"))
        {
            key.SetValue("ShowInShell", /* your value */, RegistryValueKind.DWord);
        }
    }
}
Rasmus Faber
  • 48,631
  • 24
  • 141
  • 189
  • Thanks for this, I am getting a nullReferenceException on key.SetValue() now –  Feb 19 '12 at 18:32
  • @Ben Roeves: Try CreateSubKey instead of OpenSubKey instead. Otherwise let me know what the stacktrace say. – Rasmus Faber Feb 19 '12 at 19:09
  • I am sorry, not sure what stacktrace is.... here is a link to the code with your proposed changes [codepad link](http://codepad.org/s6e3prb1) –  Feb 22 '12 at 10:40
  • @Ben Roeves: When you get an exception it will be accompanied by a stacktrace that explains where the problem occurred. It will be several lines, each starting with "at". I.e. "at Microsoft.Win32.RegistryKey.EnsureWriteable()". The linked code does not use `CreateSubKey()` - is that a mistake? `OpenSubKey()` will return null, if you do not have permission to open the key, so I guess that is what happens. – Rasmus Faber Feb 22 '12 at 12:03