コード例 #1
0
        /// <summary>
        /// Sets the startup mode for a service.
        /// </summary>
        /// <param name="serviceName">The service name.</param>
        /// <param name="mode">Thye new start mode.</param>
        public static void SetStartMode(string serviceName, ServiceStartMode mode)
        {
            RegKey key;
            int    raw = 0;

            key = RegKey.Open(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\" + serviceName);

            if (key == null)
            {
                throw new InvalidOperationException(string.Format("Service [{0}] does not exist.", serviceName));
            }

            key.Close();

            switch (mode)
            {
            case ServiceStartMode.Automatic:    raw = 2; break;

            case ServiceStartMode.Manual:       raw = 3; break;

            case ServiceStartMode.Disabled:     raw = 4; break;
            }

            if (raw != 0)
            {
                RegKey.SetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\" + serviceName + ":Start", raw);
            }
        }
コード例 #2
0
        public void RegKey_Basic()
        {
            RegKey key;

            Assert.IsFalse(RegKey.Exists(@"HKEY_LOCAL_MACHINE\Software\RegTest"));
            key = RegKey.Create(@"HKEY_LOCAL_MACHINE\Software\RegTest");
            Assert.IsTrue(RegKey.Exists(@"HKEY_LOCAL_MACHINE\Software\RegTest"));

            key.Set("Test1", "value1");
            key.Set("Test2", "value2");
            key.Set("Test3", 3);

            Assert.AreEqual("value1", key.Get("Test1"));
            Assert.AreEqual("value2", key.Get("Test2"));
            Assert.AreEqual("3", key.Get("Test3"));

            key.Set("Test1", "hello");
            Assert.AreEqual("hello", key.Get("Test1"));

            Assert.AreEqual("default", key.Get("foobar", "default"));

            key.Close();

            Assert.AreEqual("hello", RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Test1"));
            Assert.AreEqual("value2", RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Test2"));
            Assert.AreEqual("3", RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Test3"));

            RegKey.SetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Test4", "Hello");
            Assert.AreEqual("Hello", RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Test4"));

            RegKey.Delete(@"HKEY_LOCAL_MACHINE\Software\RegTest");
        }
コード例 #3
0
        public void RegKey_DeleteValue()
        {
            RegKey key;

            key = RegKey.Create(@"HKEY_LOCAL_MACHINE\Software\RegTest");

            key.Set("Test1", "10");
            key.Set("Test2", "20");

            Assert.AreEqual("20", key.Get("Test2"));
            key.DeleteValue("Test2");
            Assert.IsNull(key.Get("Test2"));

            key.Close();

            RegKey.Delete(@"HKEY_LOCAL_MACHINE\Software\RegTest:Test1");
            Assert.IsNull(key.Get("Test1"));

            RegKey.Delete(@"HKEY_LOCAL_MACHINE\Software\RegTest");

            RegKey.SetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Foo", "test");
            Assert.AreEqual("test", RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Foo"));

            RegKey.Delete(@"HKEY_LOCAL_MACHINE\Software\RegTest");
        }
コード例 #4
0
        public void RegKey_SetGet_TimeSpan()
        {
            RegKey key;

            key = RegKey.Create(@"HKEY_LOCAL_MACHINE\Software\RegTest");

            key.Set("TS1", "10");
            key.Set("TS2", "10ms");
            key.Set("TS3", "hello?");

            Assert.AreEqual(TimeSpan.FromSeconds(10), key.Get("TS1", TimeSpan.FromSeconds(55)));
            Assert.AreEqual(TimeSpan.FromMilliseconds(10), key.Get("TS2", TimeSpan.FromSeconds(55)));
            Assert.AreEqual(TimeSpan.FromSeconds(55), key.Get("TS3", TimeSpan.FromSeconds(55)));

            key.Close();

            Assert.AreEqual(TimeSpan.FromSeconds(10), RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:TS1", TimeSpan.FromSeconds(55)));
            Assert.AreEqual(TimeSpan.FromMilliseconds(10), RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:TS2", TimeSpan.FromSeconds(55)));
            Assert.AreEqual(TimeSpan.FromSeconds(55), RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:TS3", TimeSpan.FromSeconds(55)));
            Assert.AreEqual(TimeSpan.FromSeconds(55), RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:TS4", TimeSpan.FromSeconds(55)));

            RegKey.SetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:TS5", TimeSpan.FromSeconds(1001));
            Assert.AreEqual(TimeSpan.FromSeconds(1001), RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:TS5", TimeSpan.FromSeconds(1)));

            RegKey.Delete(@"HKEY_LOCAL_MACHINE\Software\RegTest");
        }
コード例 #5
0
        public void RegKey_SetGet_Int()
        {
            RegKey key;

            key = RegKey.Create(@"HKEY_LOCAL_MACHINE\Software\RegTest");

            key.Set("Int1", 0);
            key.Set("Int2", 100);
            key.Set("Int3", "-100");
            key.Set("Int4", "what???");

            Assert.AreEqual(0, key.Get("Int1", 55));
            Assert.AreEqual(100, key.Get("Int2", 55));
            Assert.AreEqual(-100, key.Get("Int3", 55));
            Assert.AreEqual(55, key.Get("Int4", 55));
            Assert.AreEqual(55, key.Get("Int5", 55));

            key.Close();

            Assert.AreEqual(0, RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Int1", 55));
            Assert.AreEqual(100, RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Int2", 55));
            Assert.AreEqual(-100, RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Int3", 55));
            Assert.AreEqual(55, RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Int4", 55));
            Assert.AreEqual(55, RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Int5", 55));

            RegKey.SetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Int6", 1001);
            Assert.AreEqual(1001, RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Int6", 0));

            RegKey.Delete(@"HKEY_LOCAL_MACHINE\Software\RegTest");
        }
コード例 #6
0
        /// <summary>
        /// Restores the state to before the installation.
        /// </summary>
        /// <param name="state">The installer state.</param>
        private void Restore(IDictionary state)
        {
            try
            {
                string keyPath  = (string)state[InstallTools.GetStateKey(this, "KeyPath")];
                object orgValue = state[InstallTools.GetStateKey(this, "OrgValue")];

                if (orgValue == null)
                {
                    RegKey.Delete(keyPath);
                }
                else if (orgValue is int)
                {
                    RegKey.SetValue(keyPath, (int)orgValue);
                }
                else if (orgValue is string)
                {
                    RegKey.GetValue(keyPath, (string)orgValue);
                }
                else
                {
                    throw new InvalidOperationException("RegInstaller works only for REG_DWORD and REG_SZ registry values.");
                }
            }
            catch
            {
                // I'm going to ignore errors here since it'll often be the case
                // that the parent key has already been removed.
            }
        }
コード例 #7
0
        public void RegKey_SetGet_Bool()
        {
            RegKey key;

            key = RegKey.Create(@"HKEY_LOCAL_MACHINE\Software\RegTest");

            key.Set("Bool1", true);
            key.Set("Bool2", false);
            key.Set("Bool3", "yes");
            key.Set("Bool4", "no");
            key.Set("Bool5", "on");
            key.Set("Bool6", "off");
            key.Set("Bool7", 1);
            key.Set("Bool8", 0);
            key.Set("Bool9", "what the hell?");

            Assert.IsTrue(key.Get("Bool1", false));
            Assert.IsFalse(key.Get("Bool2", true));
            Assert.IsTrue(key.Get("Bool3", false));
            Assert.IsFalse(key.Get("Bool4", true));
            Assert.IsTrue(key.Get("Bool5", false));
            Assert.IsFalse(key.Get("Bool6", true));
            Assert.IsTrue(key.Get("Bool7", false));
            Assert.IsFalse(key.Get("Bool8", true));
            Assert.IsTrue(key.Get("Bool9", true));
            Assert.IsFalse(key.Get("Bool9", false));
            Assert.IsTrue(key.Get("Bool10", true));
            Assert.IsFalse(key.Get("Bool10", false));

            key.Close();

            Assert.IsTrue(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool1", false));
            Assert.IsFalse(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool2", true));
            Assert.IsTrue(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool3", false));
            Assert.IsFalse(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool4", true));
            Assert.IsTrue(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool5", false));
            Assert.IsFalse(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool6", true));
            Assert.IsTrue(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool7", false));
            Assert.IsFalse(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool8", true));
            Assert.IsTrue(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool9", true));
            Assert.IsFalse(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool9", false));
            Assert.IsTrue(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool10", true));
            Assert.IsFalse(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool10", false));

            RegKey.SetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool11", true);
            Assert.IsTrue(RegKey.GetValue(@"HKEY_LOCAL_MACHINE\Software\RegTest:Bool11", false));

            RegKey.Delete(@"HKEY_LOCAL_MACHINE\Software\RegTest");
        }
コード例 #8
0
 public static bool SetConfig(string KeyName, string KeyValue, RegistryValueKind RegKind = RegistryValueKind.String)
 {
     try
     {
         RegistryKey RegKey;
         RegKey = Registry.CurrentUser.CreateSubKey("Software\\NUTty UPS Client");
         RegKey.SetValue(KeyName, KeyValue);
         RegKey.Close();
     }
     catch (Exception e)
     {
         Backend.Background.WriteNUTLog("[CONFIG] Could not set registry key: " + KeyName + " wtih value " + KeyValue + "\nException:" + e);
         return(false);
     }
     Backend.Background.WriteNUTLog("[CONFIG] Set registry key " + KeyName + " with value " + KeyValue);
     return(true);
 }
コード例 #9
0
        /// <summary>
        /// Installs the registry key.
        /// </summary>
        /// <param name="state">The installer state.</param>
        public override void Install(IDictionary state)
        {
            object orgValue;

            base.Install(state);

            switch (RegKey.GetValueType(keyPath))
            {
            case WinApi.REG_NONE:

                orgValue = null;
                break;

            case WinApi.REG_DWORD:

                orgValue = RegKey.GetValue(keyPath, 0);
                break;

            case WinApi.REG_SZ:

                orgValue = RegKey.GetValue(keyPath, string.Empty);
                break;

            default:

                throw new InvalidOperationException("RegInstaller works only for REG_DWORD and REG_SZ registry values.");
            }

            state[InstallTools.GetStateKey(this, "KeyPath")]  = keyPath;
            state[InstallTools.GetStateKey(this, "OrgValue")] = orgValue;

            if (strValue != null)
            {
                RegKey.SetValue(keyPath, strValue);
            }
            else
            {
                RegKey.SetValue(keyPath, intValue);
            }
        }