// This function takes a registry key and recursively enumerates its values // and subkeys while adding each to the parent TreeNode for displaying // in the TreeView control private void PopulateListFromReg(string rootKey, string keyName, ref ListBox parent) { // make sure rootKey and keyName aren't empty if (!rootKey.Equals("")) { // list the values first string Values = Registry.EnumValues(Registry.RootKey.LocalMachine, rootKey, false); // make sure Values is not null if (Values != null) { // split the coma-delimited return string up into the individual values string[] ValList = Values.Split(','); // iterate through each value and add to the tree // if the device is recognized by GetDevicePower foreach (string curVal in ValList) { if (curVal.StartsWith("Name")) { string DeviceName = curVal.Substring(5, curVal.Length - 6); // check to see if it's recognized by GetDevicePower // if it's recognized CE_DEVICE_POWER state = new CE_DEVICE_POWER(); int result = GetDevicePower(DeviceName, 1, ref state); if (result != 2) { parent.Items.Add(DeviceName); } } } } // now list the keys string Keys = Registry.EnumValues(Registry.RootKey.LocalMachine, rootKey, true); if (Keys != null) { string[] KeyList = Keys.Split(','); // iterate through each subkey and add to the tree foreach (string curKey in KeyList) { if (!curKey.Equals("")) { string subKey = rootKey + "\\" + curKey; PopulateListFromReg(subKey, curKey, ref parent); } } } } }
private void btnGetInfo_Click(object sender, EventArgs e) { // get the gwes info string MainKey = "System\\CurrentControlSet\\Control\\Power"; string Values = Registry.EnumValues(Registry.RootKey.LocalMachine, MainKey, false); // now add the power manager info MainKey = "System\\CurrentControlSet\\Control\\Power\\Timeouts"; Values += "," + Registry.EnumValues(Registry.RootKey.LocalMachine, MainKey, false); if (!Values.Equals("")) { string[] ValList = Values.Split(','); foreach (string curVal in ValList) { if (!curVal.Equals("")) { // grab the part of the string before the first '(' string ValName = curVal.Substring(0, curVal.IndexOf("(")); string Number = curVal.Substring(curVal.LastIndexOf("d") + 1, curVal.Length - curVal.LastIndexOf("d") - 2); switch (ValName) { case "BattSuspendTimeout": txtBatteryIdleTimeout.Text = Number + " s"; break; case "ACSuspendTimeout": txtACIdleTimeout.Text = Number + " s"; break; case "WakeupPowerOff": txtWakeIdleTimeout.Text = Number + " s"; break; case "ACResumingSuspendTimeout": txtACResumeTimeout.Text = Number + " s"; break; case "BattResumingSuspendTimeout": txtBatteryResumeTimeout.Text = Number + " s"; break; default: break; } } } } }
// This function takes a registry key and recursively enumerates its values // and subkeys while adding each to the parent TreeNode for listing // in the TreeView control. private void PopulateTreeFromReg(string rootKey, string keyName, ref TreeNode parent) { // make sure rootKey and keyName aren't empty if (!rootKey.Equals("")) { // if keyName is not empty create new node for this key // otherwise its the root key so just use parent TreeNode newNode; bool Named; // true if it's the root node if (!keyName.Equals("")) { newNode = new TreeNode(keyName); Named = false; } else { // current key is the root node, so use parent // (the passed in root node) as the current node newNode = parent; Named = true; } // list the values first string Values = Registry.EnumValues(Registry.RootKey.LocalMachine, rootKey, false); // make sure Values is not null if (Values != null) { // split the coma-delimited return string up into the individual values string[] ValList = Values.Split(','); // iterate through each value and add to the tree foreach (string curVal in ValList) { if (!curVal.Equals("")) { TreeNode newVal = new TreeNode(curVal); newNode.Nodes.Add(newVal); } } } // now list the keys string Keys = Registry.EnumValues(Registry.RootKey.LocalMachine, rootKey, true); if (Keys != null) { // split up the coma-delimited key list string[] KeyList = Keys.Split(','); // iterate through each subkey and add to the tree foreach (string curKey in KeyList) { if (!curKey.Equals("")) { string subKey = rootKey + "\\" + curKey; PopulateTreeFromReg(subKey, curKey, ref newNode); } } } // now add the node to the parent if (!Named) { parent.Nodes.Add(newNode); } } }