C#でレジストリから値を読み込むコードを紹介します。
概要
レジストリから値を読み込むには、読み込み先の RegistryKey オブジェクトのGetValue() メソッドを利用します。RegistryKey オブジェクトの取得には、Registry クラスのOpenSubKeyメソッドを利用します。
プログラム
UI
下図のUIを作成します。テキストボックス2つとボタンを配置します。テキストボックスの一つは複数行のテキストボックスにします。
コード
下記のコードを記述します。基本はボタンクリック時のイベントハンドラの実装になります。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace RegistryDemoNoUAC
{
public partial class FormRegistryRead : Form
{
public FormRegistryRead()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
string KeyName = textBox1.Text;
Microsoft.Win32.RegistryKey rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\iPentec\Demo\" + KeyName);
if (rkey != null) {
string DefaultValue = (string)rkey.GetValue("");
string ProductNameValue = (string)rkey.GetValue("ProductName");
int ProductPriceValue = (int)rkey.GetValue("ProductPrice");
textBox2.Text += "既定の値: " + DefaultValue +"\r\n";
textBox2.Text += "ProductNameの値: " + ProductNameValue + "\r\n";
textBox2.Text += "ProductPriceの値: " + ProductPriceValue.ToString() + "\r\n";
}
else {
textBox2.Text = "レジストリキーが開けませんでした。";
}
}
}
}
解説
上部のテキストボックスに入力された文字列をKeyName変数に代入します。
string KeyName = textBox1.Text;
OpenSubKeyメソッドを呼び出してキーを開きます。開くキーは
\HKEY_CURRENT_USER\SOFTWARE\iPentec\Demo\(テキストボックスに入力された名称)
になります。
Microsoft.Win32.RegistryKey rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\iPentec\Demo\" + KeyName);
キーが存在する場合は、OpenSubKey の戻り値にRegistryKey オブジェクトが返りますが、キーが存在しない場合やキーが開けない場合はnullが返りますので、戻り値がnullの場合はレジストリキーが開けなかった旨のメッセージを表示します。
if (rkey != null) {
(......)
}
else {
textBox2.Text = "レジストリキーが開けませんでした。";
}
OpenSubKey の戻り値がnullではなく RegistryKeyオブジェクトが戻った場合は、GetValueメソッドを呼び出して値を取得します。GetValueメソッドの第一引数に取得する値の名称を与えます。レジストリキーの既定の値を取得する場合は、第一引数に空文字を与えます。値を取得した後テキストボックスに取得した値を表示します。
string DefaultValue = (string)rkey.GetValue("");
string ProductNameValue = (string)rkey.GetValue("ProductName");
int ProductPriceValue = (int)rkey.GetValue("ProductPrice");
textBox2.Text += "既定の値: " + DefaultValue +"\r\n";
textBox2.Text += "ProductNameの値: " + ProductNameValue + "\r\n";
textBox2.Text += "ProductPriceの値: " + ProductPriceValue.ToString() + "\r\n";
実行結果
レジストリキーと値が存在する場合
レジストリに
\HKEY_CURRENT_USER\SOFTWARE\iPentec\Demo\Penguin
キーと既定の値、ProductName、ProductPrice の値を作成します。
プロジェクトを実行します。下図のウィンドウが表示されます。
上部のテキストボックスに "Penguin" を入力します。入力後[Read Registry]ボタンをクリックします。
下部のテキストボックスに下図の値が表示されます。レジストリに保存されている値が取得できていることが確認できます。
レジストリキーが存在しない場合
レジストリキーが存在しない場合は、レジストリキーが開けない旨のメッセージが表示されます。
レジストリキーは存在し値が存在しない場合
レジストリに
\HKEY_CURRENT_USER\SOFTWARE\iPentec\Demo\Penguin
キーを作成しましが既定の値以外の値を作成しない状態にします。
プロジェクトを実行します。ウィンドウが表示されますので、テキストボックスに "Penguin" を入力し、[Read Registry]ボタンをクリックします。
レジストリキーは開かれますが、値が無いため、GetValueでnullが返り、System.NullReferenceException が発生します。
例外に対処するためには、try,catch による例外処理を実装するか、GetValue の戻り値のnullチェックをします。
補足:値が無い場合に対応するコード例
レジストリキーが存在し、値が存在しない場合に対応するコードの一例として、下記コードのようにnullチェックをする方法があります。
private void Button2_Click(object sender, EventArgs e)
{
string KeyName = textBox1.Text;
Microsoft.Win32.RegistryKey rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"SOFTWARE\iPentec\Demo\" + KeyName);
if (rkey != null) {
string DefaultValue = "";
string ProductNameValue = "";
int ProductPriceValue = 0;
if (rkey.GetValue("") != null) {
DefaultValue = (string)rkey.GetValue("");
}
if (rkey.GetValue("ProductName") != null) {
ProductNameValue = (string)rkey.GetValue("ProductName");
}
if (rkey.GetValue("ProductPrice") != null) {
ProductPriceValue = (int)rkey.GetValue("ProductPrice");
}
textBox2.Text += "既定の値: " + DefaultValue + "\r\n";
textBox2.Text += "ProductNameの値: " + ProductNameValue + "\r\n";
textBox2.Text += "ProductPriceの値: " + ProductPriceValue.ToString() + "\r\n";
}
else {
textBox2.Text = "レジストリキーが開けませんでした。";
}
}
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2019-08-20
作成日: 2019-08-17