예제 #1
0
        /// <summary>
        /// Delete site from registry.
        /// </summary>
        /// <param name="s">site</param>
        public void DeleteSite(string s)
        {
            var site = new Site(s);

            // Set rootkey to Domains, and subkey to rootdomain key.
            var rootkey = Domains;
            var subkey  = Domains.OpenSubKey(site.RootDomain, true);

            // RootDomain was not found in registry, abort
            if (subkey == null)
            {
                return;
            }

            // If subdomain is defined then change rootkey to rootdomain key, and subkey to subdomain key
            if (!string.IsNullOrEmpty(site.SubDomain))
            {
                rootkey = subkey;
                subkey  = rootkey.OpenSubKey(site.SubDomain, true);

                // SubDomain was not found in registry, abort
                if (subkey == null)
                {
                    return;
                }
            }

            // Protocol was not found among values, abort
            if (!subkey.GetValueNames().Contains(site.Protocol))
            {
                return;
            }

            // If subkey has more than one value, or if site doesn't contain a subdomain
            if (subkey.ValueCount > 1 || string.IsNullOrEmpty(site.SubDomain))
            {
                // Delete only value
                subkey.DeleteValue(site.Protocol);
            }
            // Or if subkey doesn't contain any subkeys
            else if (subkey.SubKeyCount == 0)
            {
                // Delete subkey. There are no subkeys and it contains only one value
                string name = subkey.Name.Split('\\').Last();
                rootkey.DeleteSubKey(name);
            }

            // Delete empty rootdomain key
            rootkey = Domains.OpenSubKey(site.RootDomain, true);
            if (rootkey.SubKeyCount == 0 && rootkey.ValueCount == 0)
            {
                Domains.DeleteSubKey(site.RootDomain);
            }
        }