(registry access) How do I do this?

  • Thread starter Thread starter vbMark
  • Start date Start date
V

vbMark

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?

Thanks
 
Do this

listBox.Items.Add( strKeys.GetValue(i) + " " +
val.GetValue(sKey).ToString() );
 
I tried that and the error is just as if the .ToString() was not there.

Thanks though.

Do this

listBox.Items.Add( strKeys.GetValue(i) + " " +
val.GetValue(sKey).ToString() );

--
Shak
(Houston)




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?

Thanks
 
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.
 
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
 
<snip - this would have been rather smaller as a 10-20 line console
app, but never mind>

Right.

Problem 1:
sKey = strKeys.GetValue(i);

sKey here is of type RegistryKey (what does the s stand for here?) and
strKeys is a string array. Array.GetValue(int) returns type object, not
type RegistryKey, so that line fails to compile, and probably isn't
what you mean anyway.

Problem 2:
listBox.Items.Add( strKeys.GetValue(i) + " " + val.GetValue(sKey) );

In particular, val.GetValue(sKey). Again, sKey is of type RegistryKey,
and there's no method call RegistryKey.GetValue(RegistryKey).
 
<snip - this would have been rather smaller as a 10-20 line console
app, but never mind>

Right.

Problem 1:
sKey = strKeys.GetValue(i);

sKey here is of type RegistryKey (what does the s stand for here?) and
strKeys is a string array. Array.GetValue(int) returns type object, not
type RegistryKey, so that line fails to compile, and probably isn't
what you mean anyway.

Problem 2:
listBox.Items.Add( strKeys.GetValue(i) + " " + val.GetValue(sKey) );

In particular, val.GetValue(sKey). Again, sKey is of type RegistryKey,
and there's no method call RegistryKey.GetValue(RegistryKey).

The s in sKey is just a holdover from previous testing.

Anyway, I know there are things wrong here. How do I get it to work?
 
vbMark said:
The s in sKey is just a holdover from previous testing.

And that's just one reason to avoid Hungarian notation... it gets out
of date so quickly.
Anyway, I know there are things wrong here. How do I get it to work?

I *suspect* you meant sKey to be a string rather than a RegistryKey -
but now you know why the compiler's complaining, you should be able to
work out what exactly you want to do, and fix it that way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top