using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using log4net;

namespace ChuPiao.Common.Utils
{
    /// <summary>
    /// 注册表工具类
    /// </summary>
    public class RegistryUtil
    {
        private static ILog _log = LogManager.GetLogger("default");

        /// <summary>
        /// 在注册表中配置自动启动项
        /// </summary>
        /// <param name="key">注册项键值</param>
        /// <param name="applicationPath">.exe文件的完整路径（包含.exe文件名）</param>
        /// <returns>是否成功</returns>
        public static bool ConfigureAutoRun(string key,string applicationPath)
        {
            RegistryKey autoRunItems = null;
            try
            {
                autoRunItems = Registry.LocalMachine.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");
                string value = (string)autoRunItems.GetValue(key, "no");
                if ("no" == value || applicationPath != value)
                {
                    autoRunItems.SetValue(key, applicationPath);
                    return true;
                }
            }
            catch (Exception e) 
            {
                _log.Warn("【设置开机自动运行】失败：" + e.Message);
            }
            finally
            {
                if (autoRunItems != null)
                {
                    autoRunItems.Close();
                }
            }
            return false;
        }
        /// <summary>
        ///  在注册表中取消自动启动项
        /// </summary>
        /// <param name="key">注册项键值</param>
        /// <returns>是否成功</returns>
        public static bool CancelAutoRun(string key)
        {
            RegistryKey autoRunItems = null;
            try
            {
                autoRunItems = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
                string[] itemNames = autoRunItems.GetValueNames();
                foreach (string name in itemNames)
                {
                    if (key.ToUpper() == name.ToUpper())
                    {
                        autoRunItems.DeleteValue(name);
                        return true;
                    }
                }
            }
            catch (Exception e) 
            {
                _log.Warn("【设置开机自动运行】失败：" + e.Message);
            }
            finally
            {
                if (autoRunItems != null)
                {
                    autoRunItems.Close();
                }
            }
            return false;
        }
        /// <summary>
        /// 检查开机自动启动注册表中是否存在指定的键值对
        /// </summary>
        /// <param name="key">键名</param>
        /// <param name="applicationPath">.exe文件的完整路径（包含.exe文件名）</param>
        /// <returns>是否存在</returns>
        public static bool AutoRunItemExits(string key,string applicationPath)
        {
            RegistryKey autoRunItems = null;
            try
            {
                autoRunItems = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run",true);
                return RegistryItemExits(autoRunItems, key, applicationPath);
            }
            catch (Exception e) { }
            finally
            {
                if (autoRunItems != null)
                {
                    autoRunItems.Close();
                }
            }
            return false;
        }
        /// <summary>
        /// 检查注册表中是否存在指定的键值对
        /// </summary>
        /// <param name="subKey">注册表中顶级节点对象</param>
        /// <param name="keyName">键名</param>
        /// <returns>是否存在</returns>
        private static bool RegistryItemExits(RegistryKey subKey,string keyName,string value)
        {
            string[] itemNames = subKey.GetValueNames();
            foreach (string name in itemNames)
            {
                if (keyName.ToUpper() == name.ToUpper())
                {
                    string targetValue = (string)subKey.GetValue(name);
                    if (value != targetValue)
                    {
                        subKey.DeleteValue(name);
                        return false;
                    }
                    return true;
                }
            }
            return false;
        }
    }
}
