vbMark said:
Here's my code:
string[] strKeys;
RegistryKey rk = Registry.CurrentUser;
RegistryKey val = Registry.CurrentUser;
string sLocation = "Software\\Microsoft\\Windows\\CurrentVersion
\\Internet Settings\\P3P\\History";
rk = rk.OpenSubKey(sLocation);
strKeys = rk.GetSubKeyNames();
for (int i = 0; i < strKeys.Length; i++)
{
listBox.Items.Add( strKeys.GetValue(i) );
}
The above works to just display the Key names. However, when I try
to change the for loop to diplay the 1st value of each key I can not
get it to work. I've tried all sorts of things. Here is one example:
for (int i = 0; i < strKeys.Length; i++)
{
val = rk.OpenSubKey( sLocation + "\\" + strKeys.GetValue(i) );
sKey = strKeys.GetValue(i);
listBox.Items.Add( strKeys.GetValue(i) + " " + val.GetValue(sKey)
);
}
The above gives conversion error messages.
How do I make this work?
What error messages are you getting?
Could you post a short but complete program which demonstrates the
problem?
See
http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.Win32;
namespace CookieHelper
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class frmMain : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.ListBox listBox;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after
InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.listBox = new System.Windows.Forms.ListBox();
this.btnExit = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// listBox
//
this.listBox.Anchor =
((System.Windows.Forms.AnchorStyles)
((((System.Windows.Forms.AnchorStyles.Top |
System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listBox.Location = new System.Drawing.Point(0, 0);
this.listBox.MultiColumn = true;
this.listBox.Name = "listBox";
this.listBox.Size = new System.Drawing.Size(416, 238);
this.listBox.TabIndex = 0;
//
// btnExit
//
this.btnExit.Anchor =
((System.Windows.Forms.AnchorStyles)
((System.Windows.Forms.AnchorStyles.Bottom |
System.Windows.Forms.AnchorStyles.Right)));
this.btnExit.Location = new System.Drawing.Point(336,
248);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(75, 24);
this.btnExit.TabIndex = 1;
this.btnExit.Text = "E&xit";
this.btnExit.Click += new System.EventHandler
(this.btnExit_Click);
//
// frmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5,
13);
this.ClientSize = new System.Drawing.Size(416, 277);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.listBox);
this.Name = "frmMain";
this.Text = "vbMark\'s Cookie Helper";
this.Load += new System.EventHandler
(this.frmMain_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmMain());
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void frmMain_Load(object sender, System.EventArgs e)
{
GetValues();
}
private void GetValues()
{
// Set the selection mode to multiple and extended.
listBox.SelectionMode = SelectionMode.MultiExtended;
string[] strKeys;
//string[] strValues;
RegistryKey sKey;
RegistryKey rk = Registry.CurrentUser;
RegistryKey val = Registry.CurrentUser;
string sLocation = "Software\\Microsoft\\Windows
\\CurrentVersion\\Internet Settings\\P3P\\History";
rk = rk.OpenSubKey(sLocation);
strKeys = rk.GetSubKeyNames();
for (int i = 0; i < strKeys.Length; i++)
{
val = rk.OpenSubKey( sLocation + "\\" +
strKeys.GetValue(i) );
sKey = strKeys.GetValue(i);
listBox.Items.Add( strKeys.GetValue(i) + " " +
val.GetValue(sKey) );
}
}
}
}
Error messages:
Argument '1': cannot convert from 'Microsoft.Win32.RegistryKey' to
'string'
Cannot implicitly convert type 'string' to 'Microsoft.Win32.RegistryKey'
The best overloaded method match for
'Microsoft.Win32.RegistryKey.GetValue(string)' has some invalid arguments