Registry.GetValue メソッドとは? わかりやすく解説

Registry.GetValue メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定したレジストリ キー含まれる指定した前に関連付けられた値を取得します指定したキー該当する名前が見つからない場合は、設定している既定値返されます。指定したキー存在しない場合は、null 参照 (Visual Basic では Nothing) が返されます。

名前空間: Microsoft.Win32
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function GetValue
 ( _
    keyName As String, _
    valueName As String, _
    defaultValue As Object _
) As Object
Dim keyName As String
Dim valueName As String
Dim defaultValue As Object
Dim returnValue As Object

returnValue = Registry.GetValue(keyName, valueName, defaultValue)
public static Object GetValue (
    string keyName,
    string valueName,
    Object defaultValue
)
public:
static Object^ GetValue (
    String^ keyName, 
    String^ valueName, 
    Object^ defaultValue
)
public static Object GetValue (
    String keyName, 
    String valueName, 
    Object defaultValue
)
public static function GetValue
 (
    keyName : String, 
    valueName : String, 
    defaultValue : Object
) : Object

パラメータ

keyName

有効なレジストリ ルート ("HKEY_CURRENT_USER" など) から始まるキーの完全なレジストリ パス

valueName

名前/値ペアの名前。

defaultValue

name存在しない場合返す値。

戻り値
keyName指定したサブキーが存在しない場合は、null 参照 (Visual Basic では Nothing)。それ以外場合は、valueName関連付けられた値。valueName が見つからない場合は、defaultValue

例外例外
解説解説

文字列 valueName では、大文字と小文字区別されません。

メモメモ

レジストリ キーには、どの名前に関連付けられていない値を 1 つ格納できます。この無名の値がレジストリ エディタ表示されるときには、名前の代わりに "(既定)" という文字列表示されます。この無名の値を取得するには、valuNamenull 参照 (Visual Basic では Nothing) または空の文字列 ("") を指定します

有効なルート名は、HKEY_CURRENT_USER、HKEY_LOCAL_MACHINE、HKEY_CLASSES_ROOT、HKEY_USERS、HKEY_PERFORMANCE_DATA、HKEY_CURRENT_CONFIG、および HKEY_DYN_DATA です。たとえば、Visual Basic では、文字列 "HKEY_CURRENT_USER\MyTestKey" は、HKEY_CURRENT_USER ルートのサブキー "MyTestKey" のキー/値ペアアクセスます。

GetValue メソッドが展開可能な文字列値 (RegistryValueKind.ExpandString) を取得するときにはローカル環境データ使用して環境文字列展開します環境変数への展開可能な参照含まれた値を、展開可能な文字列 (RegistryValueKind.ExpandString) としてではなく文字列 (RegistryValueKind.String) として格納している場合GetValue はこの値を展開しません。このような文字列展開するには、Environment.ExpandEnvironmentVariables メソッド呼び出して文字列取得しておきます

メモメモ

HKEY_PERFORMANCE_DATA からデータ取得する場合は、Microsoft.Win32.RegistryKey.GetValue メソッドではなくPerformanceCounter クラス使用することをお勧めます。

GetValue メソッドと SetValue メソッドは、使用されるたびにレジストリ キー開いた閉じたりするため、多数の値にアクセスする場合に、RegistryKey クラスメソッドと共に実行されることはありません。

RegistryKey には、レジストリ キーアクセス制御リスト (ACL: Access Cntrol List) を追加できるメソッド、値を取得する前にそのデータ型テストできるメソッドキー削除できるメソッド用意されています。

使用例使用例

サンプル キー作成し、このキー複数データ型の値を格納した後、その値を取得して表示するコード例次に示します。この例では、既定の (無名の) 名前/値ペア並べ替え取得する方法、および名前/値ペアが存在しない場合defaultValue使用方法示してます。

Imports System
Imports Microsoft.Win32

Public Class Example
    Public Shared Sub Main()
        ' The name of the key must include a valid root.
        Const userRoot As String
 = "HKEY_CURRENT_USER"
        Const subkey As String
 = "RegistrySetValueExample"
        Const keyName As String
 = userRoot & "\" & subkey

        ' Integer values can be stored without specifying the
        ' registry data type, but Long values will be stored
        ' as strings unless you specify the type. Note that
        ' the integer is stored in the default name/value
        ' pair.
        Registry.SetValue(keyName, "", 5280)
        Registry.SetValue(keyName, "TestLong", 12345678901234,
 _
            RegistryValueKind.QWord)

        ' Strings with expandable environment variables are
        ' stored as ordinary strings unless you specify the
        ' data type.
        Registry.SetValue(keyName, "TestExpand", "My
 path: %path%")
        Registry.SetValue(keyName, "TestExpand2",
 "My path: %path%", _
            RegistryValueKind.ExpandString)

        ' Arrays of strings are stored automatically as 
        ' MultiString. Similarly, arrays of Byte are stored
        ' automatically as Binary.
        Dim strings() As String
 = {"One", "Two",
 "Three"}
        Registry.SetValue(keyName, "TestArray", strings)

        ' Your default value is returned if the name/value pair
        ' does not exist.
        Dim noSuch As String
 = _
            Registry.GetValue(keyName, "NoSuchName",
 _
            "Return this default if NoSuchName does not exist.")
        Console.WriteLine(vbCrLf & "NoSuchName: {0}",
 noSuch)

        ' Retrieve the Integer and Long values, specifying 
        ' numeric default values in case the name/value pairs
        ' do not exist. The Integer value is retrieved from the
        ' default (nameless) name/value pair for the key.
        Dim tInteger As Integer
 = _
            Registry.GetValue(keyName, "", -1)
        Console.WriteLine("(Default): {0}", tInteger)
        Dim tLong As Long
 = Registry.GetValue(keyName, _
             "TestLong", Long.MinValue)
        Console.WriteLine("TestLong: {0}", tLong)

        ' When retrieving a MultiString value, you can specify
        ' an array for the default return value. The value is
        ' declared inline, but could also be declared as:
        ' Dim default() As String = {"Default value."}
        '
        Dim tArray() As String
 = _
            Registry.GetValue(keyName, "TestArray",
 _
            New String() {"Default
 if TestArray does not exist."})
        For i As Integer
 = 0 To tArray.Length - 1
            Console.WriteLine("TestArray({0}): {1}",
 i, tArray(i))
        Next

        ' A string with embedded environment variables is not
        ' expanded if it was stored as an ordinary string.
        Dim tExpand As String
 = Registry.GetValue(keyName, _
             "TestExpand", "Default
 if TestExpand does not exist.")
        Console.WriteLine("TestExpand: {0}", tExpand)

        ' A string stored as ExpandString is expanded.
        Dim tExpand2 As String
 = Registry.GetValue(keyName, _
             "TestExpand2", "Default
 if TestExpand2 does not exist.")
        Console.WriteLine("TestExpand2: {0}...", _
            tExpand2.Substring(0, 40))

        Console.WriteLine(vbCrLf & _
            "Use the registry editor to examine the key.")
        Console.WriteLine("Press the Enter key to delete the key.")
        Console.ReadLine()
        Registry.CurrentUser.DeleteSubKey(subkey)
    End Sub
End Class
'
' This code example produces output similar to the following:
'
'NoSuchName: Return this default if NoSuchName does not exist.
'(Default): 5280
'TestLong: 12345678901234
'TestArray(0): One
'TestArray(1): Two
'TestArray(2): Three
'TestExpand: My path: %path%
'TestExpand2: My path: D:\Program Files\Microsoft.NET\...
'
'Use the registry editor to examine the key.
'Press the Enter key to delete the key.
using System;
using Microsoft.Win32;

public class Example
{
    public static void Main()
    {
        // The name of the key must include a valid root.
        const string userRoot = "HKEY_CURRENT_USER";
        const string subkey = "RegistrySetValueExample";
        const string keyName = userRoot + "\\"
 + subkey;

        // An int value can be stored without specifying the
        // registry data type, but long values will be stored
        // as strings unless you specify the type. Note that
        // the int is stored in the default name/value
        // pair.
        Registry.SetValue(keyName, "", 5280);
        Registry.SetValue(keyName, "TestLong", 12345678901234,
            RegistryValueKind.QWord);

        // Strings with expandable environment variables are
        // stored as ordinary strings unless you specify the
        // data type.
        Registry.SetValue(keyName, "TestExpand", "My path: %path%");
        Registry.SetValue(keyName, "TestExpand2", "My path: %path%"
,
            RegistryValueKind.ExpandString);

        // Arrays of strings are stored automatically as 
        // MultiString. Similarly, arrays of Byte are stored
        // automatically as Binary.
        string[] strings = {"One", "Two",
 "Three"};
        Registry.SetValue(keyName, "TestArray", strings);

        // Your default value is returned if the name/value pair
        // does not exist.
        string noSuch = (string) Registry.GetValue(keyName,
 
            "NoSuchName",
            "Return this default if
 NoSuchName does not exist.");
        Console.WriteLine("\r\nNoSuchName: {0}", noSuch);

        // Retrieve the int and long values, specifying 
        // numeric default values in case the name/value pairs
        // do not exist. The int value is retrieved from the
        // default (nameless) name/value pair for the key.
        int tInteger = (int) Registry.GetValue(keyName,
 "", -1);
        Console.WriteLine("(Default): {0}", tInteger);
        long tLong = (long) Registry.GetValue(keyName, "TestLong",
            long.MinValue);
        Console.WriteLine("TestLong: {0}", tLong);

        // When retrieving a MultiString value, you can specify
        // an array for the default return value. 
        string[] tArray = (string[]) Registry.GetValue(keyName
,
            "TestArray",
            new string[] {"Default if
 TestArray does not exist."});
        for(int i=0; i<tArray.Length; i++)
        {
            Console.WriteLine("TestArray({0}): {1}", i, tArray[i]);
        }

        // A string with embedded environment variables is not
        // expanded if it was stored as an ordinary string.
        string tExpand = (string) Registry.GetValue(keyName
,
             "TestExpand", 
             "Default if TestExpand does not exist.");
        Console.WriteLine("TestExpand: {0}", tExpand);

        // A string stored as ExpandString is expanded.
        string tExpand2 = (string) Registry.GetValue(keyName
,
            "TestExpand2",
            "Default if TestExpand2 does not exist.");
        Console.WriteLine("TestExpand2: {0}...",
            tExpand2.Substring(0, 40));

        Console.WriteLine("\r\nUse the registry editor to examine the key.");
        Console.WriteLine("Press the Enter key to delete the key.");
        Console.ReadLine();
        Registry.CurrentUser.DeleteSubKey(subkey);
    }
}
//
// This code example produces output similar to the following:
//
//NoSuchName: Return this default if NoSuchName does not exist.
//(Default): 5280
//TestLong: 12345678901234
//TestArray(0): One
//TestArray(1): Two
//TestArray(2): Three
//TestExpand: My path: %path%
//TestExpand2: My path: D:\Program Files\Microsoft.NET\...
//
//Use the registry editor to examine the key.
//Press the Enter key to delete the key.
using namespace System;
using namespace Microsoft::Win32;

int main()
{   
    // The name of the key must include a valid root.
    String^ userRoot = "HKEY_CURRENT_USER";
    String^ subKey = "RegistrySetValueExample2";
    String^ keyName = String::Concat(userRoot, "\\", subKey);
    
    // An int value can be stored without specifying the
    // registry data type, but Int64 values will be stored
    // as strings unless you specify the type. Note that
    // the int is stored in the default name/value
    // pair.
    Registry::SetValue(keyName, "", 5280);
    Registry::SetValue(keyName, "TestInt64", 12345678901234, 
        RegistryValueKind::QWord);
    
    // Strings with expandable environment variables are
    // stored as ordinary strings unless you specify the
    // data type.
    Registry::SetValue(keyName, "TestExpand", "My path: %path%");
    Registry::SetValue(keyName, "TestExpand2", "My path: %path%",
 
        RegistryValueKind::ExpandString);
    
    // Arrays of strings are stored automatically as 
    // MultiString. Similarly, arrays of Byte are stored
    // automatically as Binary.
    array<String^>^ strings  = {"One", "Two", "Three"};
    Registry::SetValue(keyName, "TestArray", strings);
    
    // Your default value is returned if the name/value pair
    // does not exist.
    String^ noSuch = (String^)Registry::GetValue(keyName, 
        "NoSuchName", 
        "Return this default if
 NoSuchName does not exist.");
    Console::WriteLine("\r\nNoSuchName: {0}", noSuch);
    
    // Retrieve the int and Int64 values, specifying 
    // numeric default values in case the name/value pairs
    // do not exist. The int value is retrieved from the
    // default (nameless) name/value pair for the key.
    int testInteger = (int)Registry::GetValue(keyName,
 "", -1);
    Console::WriteLine("(Default): {0}", testInteger);
    long long testInt64 = (long long)Registry::GetValue(keyName, 
        "TestInt64", System::Int64::MinValue);
    Console::WriteLine("TestInt64: {0}", testInt64);
    
    // When retrieving a MultiString value, you can specify
    // an array for the default return value. 
    array<String^>^ testArray = (array<String^>^)Registry::GetValue(
        keyName, "TestArray", 
        gcnew array<String^> {"Default if TestArray
 does not exist."});
    for (int i = 0; i < testArray->Length;
 i++)
    {
        Console::WriteLine("TestArray({0}): {1}", i, testArray[i]);
    }
    
    // A string with embedded environment variables is not
    // expanded if it was stored as an ordinary string.
    String^ testExpand = (String^)Registry::GetValue(keyName, 
        "TestExpand", "Default if TestExpand does
 not exist.");
    Console::WriteLine("TestExpand: {0}", testExpand);
    
    // A string stored as ExpandString is expanded.
    String^ testExpand2 = (String^)Registry::GetValue(keyName, 
        "TestExpand2", "Default if TestExpand2
 does not exist.");
    Console::WriteLine(
        "TestExpand2: {0}...", testExpand2->Substring(0, 40));
    Console::WriteLine(
        "\r\nUse the registry editor to examine the key.");
    Console::WriteLine("Press the Enter key to delete the key.");
    Console::ReadLine();
    Registry::CurrentUser->DeleteSubKey(subKey);
}
//
// This code example produces output similar to the following:
//
// NoSuchName: Return this default if NoSuchName does not exist.
// (Default): 5280
// TestInt64: 12345678901234
// TestArray(0): One
// TestArray(1): Two
// TestArray(2): Three
// TestExpand: My path: %path%
// TestExpand2: My path: D:\Program Files\Microsoft.NET\...
//
// Use the registry editor to examine the key.
// Press the Enter key to delete the key.
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からRegistry.GetValue メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からRegistry.GetValue メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からRegistry.GetValue メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Registry.GetValue メソッド」の関連用語

Registry.GetValue メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Registry.GetValue メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS