public void CanReadRegistryGivenKeyName()
 {
     using (
         var source = new RegistryConfigurationSource( string.Format( "{0}\\{1}", LocalMachineRoot, KeyName ) ) )
     {
     }
 }
        public void CanLoadKeys()
        {
            try
            {
                using (var source = new RegistryConfigurationSource(FullTestKeyName(TestKeyName)))
                {
                    source.Sections[SectionName].Set("key", "value");
                    source.Add(new ConfigurationSection("NewSettings"));
                    source.Sections["NewSettings"].Set("count", 5);
                    source.Save();
                }
                using (var source = new RegistryConfigurationSource(FullTestKeyName(TestKeyName)))
                {
                    Assert.Equal(2, source.Sections.Count);

                    Assert.Equal("NewSettings", source.Sections.ToList()[0].Value.Name);
                    Assert.Equal(5, source.Sections["NewSettings"].Get <int>("count"));

                    Assert.Equal(SectionName, source.Sections.ToList()[1].Value.Name);
                    Assert.Equal("value", source.Sections[SectionName].Get <string>("key"));
                }
            }
            finally
            {
                DeleteKey(TestKeyName);
            }
        }
        public void CanAddMultiLevelKeys()
        {
            try
            {
                using (var source = new RegistryConfigurationSource(FullTestKeyName(TestKeyName)))
                {
                    source.Sections[SectionName].Set("key", "value");
                    source.Add(new ConfigurationSection("NewSettings"));
                    source.Sections["NewSettings"].Set("count", 5);
                    source.Add(new ConfigurationSection("NewSettings\\Legacy"));
                    source.Sections["NewSettings\\Legacy"].Set("count", 5);
                    source.Save();
                }

                var root = TestKeyName;
                Assert.True(KeyExists(root));
                Assert.False(KeyExists(string.Format("{0}\\{1}", root, SectionName)));
                Assert.True(KeyExists(root + "\\NewSettings"));
                Assert.True(KeyExists(root + "\\NewSettings\\Legacy"));
            }
            finally
            {
                DeleteKey(TestKeyName);
            }
        }
 public void CanReadRegistryGivenKeyName()
 {
     using (
         var source = new RegistryConfigurationSource(string.Format("{0}\\{1}", LocalMachineRoot, KeyName)))
     {
     }
 }
 public void CanReadRegistryGivenKey()
 {
     using ( RegistryKey key =
         Registry.LocalMachine.OpenSubKey( KeyName ) )
     {
         using ( var source = new RegistryConfigurationSource( key ) )
         {
         }
     }
 }
 public void CanReadRegistryGivenKey()
 {
     using (RegistryKey key =
                Registry.LocalMachine.OpenSubKey(KeyName))
     {
         using (var source = new RegistryConfigurationSource(key))
         {
         }
     }
 }
 private static bool KeyExists(string keyName)
 {
     using (RegistryKey key = RegistryConfigurationSource.OpenRoot(LocalMachineRoot))
     {
         using (var target = key.OpenSubKey(keyName))
         {
             return(target != null);
         }
     }
 }
        private static void DeleteKey(string keyName)
        {
            if (!KeyExists(keyName))
            {
                return;
            }

            using (RegistryKey key = RegistryConfigurationSource.OpenRoot(LocalMachineRoot))
            {
                key.DeleteSubKeyTree(keyName);
                Assert.Null(key.OpenSubKey(keyName));
            }
        }
        public void CanWriteRegistryValueKinds()
        {
            try
            {
                CreateRegistryValueKindSamples(TestKey, SectionName);

                using (var source = new RegistryConfigurationSource(FullTestKeyName(TestKeyName)))
                {
                    var section = source.Sections[SectionName];
                    section.Set("QuadWordValue", 13);
                    section.Set("DWordValue", 13);
                    section.Set("StringValue", 13.ToString());
                    section.Set("ExpandedStringValue", "13 %PATH%");
                    section.Set("MultipleStringValue", new[] { 13.ToString(), 13.ToString() });
                    section.Set("BinaryValue", new byte[] { 13, 13 });
                    source.Save();
                }
                using (var source = new RegistryConfigurationSource(FullTestKeyName(TestKeyName)))
                {
                    var section  = source.Sections[SectionName];
                    var quadWord = section.Get <long>("QuadWordValue");
                    Assert.Equal(13, quadWord);

                    var dWord = section.Get <int>("DWordValue");
                    Assert.Equal(13, dWord);

                    var strings = section.Get <string[]>("MultipleStringValue");
                    Assert.Equal(new[] { 13.ToString(), 13.ToString() }, strings);

                    var newStringValue = section.Get <string>("StringValue");
                    Assert.Equal(13.ToString(), newStringValue);

                    var newExpandedStringValue = section.Get <string>("ExpandedStringValue");
                    Assert.NotEqual(expandedStringValue, newExpandedStringValue);
                    var realExpandedValue = "13 %PATH%".Replace("%PATH%",
                                                                Environment.GetEnvironmentVariable("PATH"));
                    Assert.Equal(realExpandedValue, newExpandedStringValue);

                    var data = section.Get <byte[]>("BinaryValue");
                    Assert.Equal(new byte[] { 13, 13 }, data);
                }
            }
            finally
            {
                DeleteKey(TestKeyName);
            }
        }
        public void CanAddNewKeys()
        {
            try
            {
                using ( var source = new RegistryConfigurationSource( FullTestKeyName( TestKeyName ) ) )
                {
                    source.Sections[SectionName].Set( "key", "value" );
                    source.Add( new ConfigurationSection( "NewSettings" ) );
                    source.Sections["NewSettings"].Set( "count", 5 );
                    source.Save();
                }

                var root = TestKeyName;
                Assert.True( KeyExists( root ) );
                Assert.False( KeyExists( string.Format( "{0}\\{1}", root, SectionName ) ) );
                Assert.True( KeyExists( root + "\\NewSettings" ) );
            }
            finally
            {
                DeleteKey( TestKeyName );
            }
        }
        public void CanReadRegistryValueKinds()
        {
            try
            {
                CreateRegistryValueKindSamples(TestKey, SectionName);

                using (var source = new RegistryConfigurationSource(FullTestKeyName(TestKeyName)))
                {
                    var section = source.Sections[SectionName];

                    var quadWord = section.Get <long>("QuadWordValue");
                    Assert.Equal(quadWordValue, quadWord);

                    var dWord = section.Get <int>("DWordValue");
                    Assert.Equal(quadWordValue, dWord);

                    var strings = section.Get <string[]>("MultipleStringValue");
                    Assert.Equal(multipleStringValue, strings);

                    var newStringValue = section.Get <string>("StringValue");
                    Assert.Equal(stringValue, newStringValue);

                    var newExpandedStringValue = section.Get <string>("ExpandedStringValue");
                    Assert.NotEqual(expandedStringValue, newExpandedStringValue);
                    var realExpandedValue = expandedStringValue.Replace("%PATH%",
                                                                        Environment.GetEnvironmentVariable("PATH"));
                    Assert.Equal(realExpandedValue, newExpandedStringValue);

                    var data = section.Get <byte[]>("BinaryValue");
                    Assert.Equal(binaryValue, data);
                }
            }
            finally
            {
                DeleteKey(TestKeyName);
            }
        }
コード例 #12
0
        }   // End Constructor

        public RegistryConfigurationProvider(RegistryConfigurationSource source)
            : this()
        {
            this.m_source = source;
        } // End Constructor
        public void CanLoadKeys()
        {
            try
            {
                using ( var source = new RegistryConfigurationSource( FullTestKeyName( TestKeyName ) ) )
                {
                    source.Sections[SectionName].Set( "key", "value" );
                    source.Add( new ConfigurationSection( "NewSettings" ) );
                    source.Sections["NewSettings"].Set( "count", 5 );
                    source.Save();
                }
                using ( var source = new RegistryConfigurationSource( FullTestKeyName( TestKeyName ) ) )
                {
                    Assert.Equal( 2, source.Sections.Count );

                    Assert.Equal( "NewSettings", source.Sections.ToList()[0].Value.Name );
                    Assert.Equal( 5, source.Sections["NewSettings"].Get<int>( "count" ) );

                    Assert.Equal( SectionName, source.Sections.ToList()[1].Value.Name );
                    Assert.Equal( "value", source.Sections[SectionName].Get<string>( "key" ) );
                }
            }
            finally
            {
                DeleteKey( TestKeyName );
            }
        }
        public void CanWriteRegistryValueKinds()
        {
            try
            {
                CreateRegistryValueKindSamples( TestKey, SectionName );

                using ( var source = new RegistryConfigurationSource( FullTestKeyName( TestKeyName ) ) )
                {
                    var section = source.Sections[SectionName];
                    section.Set( "QuadWordValue", 13 );
                    section.Set( "DWordValue", 13 );
                    section.Set( "StringValue", 13.ToString() );
                    section.Set( "ExpandedStringValue", "13 %PATH%" );
                    section.Set( "MultipleStringValue", new[] {13.ToString(), 13.ToString()} );
                    section.Set( "BinaryValue", new byte[] {13, 13} );
                    source.Save();
                }
                using ( var source = new RegistryConfigurationSource( FullTestKeyName( TestKeyName ) ) )
                {
                    var section = source.Sections[SectionName];
                    var quadWord = section.Get<long>( "QuadWordValue" );
                    Assert.Equal( 13, quadWord );

                    var dWord = section.Get<int>( "DWordValue" );
                    Assert.Equal( 13, dWord );

                    var strings = section.Get<string[]>( "MultipleStringValue" );
                    Assert.Equal( new[] {13.ToString(), 13.ToString()}, strings );

                    var newStringValue = section.Get<string>( "StringValue" );
                    Assert.Equal( 13.ToString(), newStringValue );

                    var newExpandedStringValue = section.Get<string>( "ExpandedStringValue" );
                    Assert.NotEqual( expandedStringValue, newExpandedStringValue );
                    var realExpandedValue = "13 %PATH%".Replace( "%PATH%",
                                                                 Environment.GetEnvironmentVariable( "PATH" ) );
                    Assert.Equal( realExpandedValue, newExpandedStringValue );

                    var data = section.Get<byte[]>( "BinaryValue" );
                    Assert.Equal( new byte[] {13, 13}, data );
                }
            }
            finally
            {
                DeleteKey( TestKeyName );
            }
        }
        public void CanReadRegistryValueKinds()
        {
            try
            {
                CreateRegistryValueKindSamples( TestKey, SectionName );

                using ( var source = new RegistryConfigurationSource( FullTestKeyName( TestKeyName ) ) )
                {
                    var section = source.Sections[SectionName];

                    var quadWord = section.Get<long>( "QuadWordValue" );
                    Assert.Equal( quadWordValue, quadWord );

                    var dWord = section.Get<int>( "DWordValue" );
                    Assert.Equal( quadWordValue, dWord );

                    var strings = section.Get<string[]>( "MultipleStringValue" );
                    Assert.Equal( multipleStringValue, strings );

                    var newStringValue = section.Get<string>( "StringValue" );
                    Assert.Equal( stringValue, newStringValue );

                    var newExpandedStringValue = section.Get<string>( "ExpandedStringValue" );
                    Assert.NotEqual( expandedStringValue, newExpandedStringValue );
                    var realExpandedValue = expandedStringValue.Replace( "%PATH%",
                                                                         Environment.GetEnvironmentVariable( "PATH" ) );
                    Assert.Equal( realExpandedValue, newExpandedStringValue );

                    var data = section.Get<byte[]>( "BinaryValue" );
                    Assert.Equal( binaryValue, data );
                }
            }
            finally
            {
                DeleteKey( TestKeyName );
            }
        }