Csharp/CSharp Tutorial/Windows/Registry Read — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:20, 26 мая 2010
Содержание
Get int value from Registry
using System;
using Microsoft.Win32;
class MainClass
{
public static void Main(String[] args)
{
int Count = (Int32)Registry.GetValue(
@"HKEY_CURRENT_USER\Software\CompanyName\SoftwareName",
"Count", 0); // 0 is the default value
}
}
Get Registry values with default
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
using System.Diagnostics;
public class MainClass{
public static void Main(){
RegistryKey regKey = Registry.CurrentUser;
regKey = regKey.CreateSubKey("Software\\YourCompanyName\\SubDomainName");
int currFontSize = (int)regKey.GetValue("CurrSize", 12);
string c = (string)regKey.GetValue("CurrColor", "defaultName");
}
}
Loop through all subkeys contained in the current key
using System;
using Microsoft.Win32;
class MainClass
{
public static void SearchSubKeys(RegistryKey root, String searchKey)
{
foreach (string keyname in root.GetSubKeyNames())
{
try
{
using (RegistryKey key = root.OpenSubKey(keyname))
{
if (keyname == searchKey)
Console.WriteLine("Registry key found : {0} contains {1} values",
key.Name, key.ValueCount);
SearchSubKeys(key, searchKey);
}
}
catch (System.Security.SecurityException)
{
}
}
}
public static void Main(String[] args)
{
using (RegistryKey root = Registry.CurrentUser)
{
string myKey="Java";
SearchSubKeys(root, myKey);
}
}
}
Loop through the values inside a key and display
using System;
using Microsoft.Win32;
class MainClass
{
public static void SearchSubKeys(RegistryKey root, String searchKey)
{
foreach (string keyname in root.GetSubKeyNames())
{
try
{
using (RegistryKey key = root.OpenSubKey(keyname))
{
if (keyname == searchKey) {
foreach (string valuename in key.GetValueNames())
{
if (key.GetValue(valuename) is String)
{
Console.WriteLine(" Value : {0} = {1}",
valuename, key.GetValue(valuename));
}
}
}
SearchSubKeys(key, searchKey);
}
}
catch (System.Security.SecurityException)
{
}
}
}
public static void Main(String[] args)
{
using (RegistryKey root = Registry.CurrentUser)
{
string myKey="Java";
SearchSubKeys(root, myKey);
}
}
}
Registry Tree
using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
public class RegistryBrowser : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView tvRegistry;
private System.Windows.Forms.ImageList ilTreeImages;
public RegistryBrowser()
{
InitializeComponent();
RootNodes();
}
private void InitializeComponent()
{
this.tvRegistry = new System.Windows.Forms.TreeView();
this.ilTreeImages = new System.Windows.Forms.ImageList();
this.SuspendLayout();
//
// tvRegistry
//
this.tvRegistry.Dock = System.Windows.Forms.DockStyle.Fill;
this.tvRegistry.Name = "tvRegistry";
this.tvRegistry.Size = new System.Drawing.Size(392, 333);
this.tvRegistry.TabIndex = 0;
this.tvRegistry.BeforeExpand += new System.Windows.Forms.TreeViewCancelEventHandler(this.tvRegistry_BeforeExpand);
//
// ilTreeImages
//
this.ilTreeImages.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
this.ilTreeImages.TransparentColor = System.Drawing.Color.Transparent;
//
// RegistryBrowser
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(392, 333);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.tvRegistry});
this.Name = "RegistryBrowser";
this.Text = "Registry Browser";
this.ResumeLayout(false);
}
static void Main()
{
Application.Run(new RegistryBrowser());
}
private void tvRegistry_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
{
tvRegistry.BeginUpdate();
foreach (TreeNode tn in e.Node.Nodes)
{
AddBranch(tn);
}
tvRegistry.EndUpdate();
}
public void AddBranch(TreeNode tn)
{
tn.Nodes.Clear();
string strRegistryPath = tn.FullPath;
RegistryKey regBranch = null;
if (strRegistryPath.StartsWith("HKEY_CLASSES_ROOT"))
regBranch = Registry.ClassesRoot;
else if (strRegistryPath.StartsWith("HKEY_CURRENT_USER"))
regBranch = Registry.CurrentUser;
else if (strRegistryPath.StartsWith("HKEY_LOCAL_MACHINE"))
regBranch = Registry.LocalMachine;
else if (strRegistryPath.StartsWith("HKEY_USERS"))
regBranch = Registry.Users;
RegistryKey regSubKey = null;
try
{
if (null != tn.Parent)
{
// we need the path minus the top level tree
int nPosPathSeparator = strRegistryPath.IndexOf(this.tvRegistry.PathSeparator);
string strSubkey = strRegistryPath.Substring(nPosPathSeparator+1);
regSubKey = regBranch.OpenSubKey(strSubkey);
}
else
regSubKey = regBranch;
}
catch
{
return;
}
string[] astrSubkeyNames = regSubKey.GetSubKeyNames();
for (int i=0; i < astrSubkeyNames.Length; i++)
{
TreeNode tnBranch = new TreeNode(astrSubkeyNames[i],0,1);
tn.Nodes.Add(tnBranch);
}
}
public void RootNodes()
{
tvRegistry.BeginUpdate();
TreeNode tnHKCR = new TreeNode("HKEY_CLASSES_ROOT",0,1);
tvRegistry.Nodes.Add(tnHKCR);
AddBranch(tnHKCR);
TreeNode tnHKCU = new TreeNode("HKEY_CURRENT_USER",0,1);
tvRegistry.Nodes.Add(tnHKCU);
AddBranch(tnHKCU);
TreeNode tnHKLM = new TreeNode("HKEY_LOCAL_MACHINE",0,1);
tvRegistry.Nodes.Add(tnHKLM);
AddBranch(tnHKLM);
TreeNode tnHKU = new TreeNode("HKEY_USERS",0,1);
tvRegistry.Nodes.Add(tnHKU);
AddBranch(tnHKU);
tvRegistry.SelectedNode = tnHKLM;
tvRegistry.EndUpdate();
}
}
Registry Tree With Class
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Microsoft.Win32;
public class RegistryTreeClass: TreeView
{
public RegistryTreeClass()
{
ImageList = new ImageList();
ImageList.Images.Add(new Bitmap(GetType(), "CLOSED.BMP"));
ImageList.Images.Add(new Bitmap(GetType(), "OPEN.BMP"));
RootNodes();
}
protected override void OnBeforeExpand(TreeViewCancelEventArgs e)
{
base.OnBeforeExpand(e);
BeginUpdate();
foreach (TreeNode tn in e.Node.Nodes)
{
AddBranch(tn);
}
EndUpdate();
}
public void RootNodes()
{
BeginUpdate();
TreeNode tnHKCR = new TreeNode("HKEY_CLASSES_ROOT",0,1);
Nodes.Add(tnHKCR);
AddBranch(tnHKCR);
TreeNode tnHKCU = new TreeNode("HKEY_CURRENT_USER",0,1);
Nodes.Add(tnHKCU);
AddBranch(tnHKCU);
TreeNode tnHKLM = new TreeNode("HKEY_LOCAL_MACHINE",0,1);
Nodes.Add(tnHKLM);
AddBranch(tnHKLM);
TreeNode tnHKU = new TreeNode("HKEY_USERS",0,1);
Nodes.Add(tnHKU);
AddBranch(tnHKU);
SelectedNode = tnHKLM;
EndUpdate();
}
public void AddBranch(TreeNode tn)
{
if (tn.Nodes.Count > 0) return;
string strRegistryPath = tn.FullPath;
RegistryKey regBranch = null;
if (strRegistryPath.StartsWith("HKEY_CLASSES_ROOT"))
regBranch = Registry.ClassesRoot;
else if (strRegistryPath.StartsWith("HKEY_CURRENT_USER"))
regBranch = Registry.CurrentUser;
else if (strRegistryPath.StartsWith("HKEY_LOCAL_MACHINE"))
regBranch = Registry.LocalMachine;
else if (strRegistryPath.StartsWith("HKEY_USERS"))
regBranch = Registry.Users;
RegistryKey regSubKey = null;
try
{
if (null != tn.Parent)
{
// We need the path minus the top level tree.
int nPosPathSeparator = strRegistryPath.IndexOf(this.PathSeparator);
string strSubkey = strRegistryPath.Substring(nPosPathSeparator+1);
regSubKey = regBranch.OpenSubKey(strSubkey);
}
else
regSubKey = regBranch;
}
catch
{
return;
}
string[] astrSubkeyNames = regSubKey.GetSubKeyNames();
for (int i=0; i < astrSubkeyNames.Length; i++)
{
TreeNode tnBranch = new TreeNode(astrSubkeyNames[i],0,1);
tn.Nodes.Add(tnBranch);
}
}
}
public class MainForm : System.Windows.Forms.Form
{
private RegistryTreeClass rtvRegistry;
public MainForm()
{
InitializeComponent();
this.rtvRegistry = new RegistryTreeClass();
this.SuspendLayout();
this.rtvRegistry.Dock = System.Windows.Forms.DockStyle.Fill;
this.rtvRegistry.ImageIndex = -1;
this.rtvRegistry.Name = "rtvRegistry";
this.rtvRegistry.SelectedImageIndex = -1;
this.rtvRegistry.Size = new System.Drawing.Size(292, 273);
this.rtvRegistry.TabIndex = 0;
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.rtvRegistry});
this.ResumeLayout(false);
}
private void InitializeComponent()
{
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "MainForm";
this.Text = "Registry with class";
this.Load += new System.EventHandler(this.Form1_Load);
}
static void Main()
{
Application.Run(new MainForm());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
}