using System;
using System.Text;
using Microsoft.Win32;

namespace NewPosInstaller
{
    /// <summary>
    /// ubSys.Registry
    /// </summary>
    public class Registry
    {

        /// <summary>
        /// 레스트리 값 가져오기
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="subKey"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string GetValue(Microsoft.Win32.RegistryKey regKey, string subKey, string name)
        {
            string retValue = string.Empty; ;

            try
            {
                using (regKey = regKey.OpenSubKey(subKey, true))
                {
                    if (regKey != null)
                        retValue = System.Convert.ToString(regKey.GetValue(name));
                    regKey.Close();
                }
            }
            catch (Exception ex)
            {
                ComLog.WriteLog(ComLog.Level.Exception
                              , System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "."
                              + System.Reflection.MethodBase.GetCurrentMethod().Name + "()"
                              , ex.Message); 
                retValue = string.Empty;
            }

            return retValue;
        }



        /// <summary>
        /// 레지스트리에 값 쓰기
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="subKey"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetValue(Microsoft.Win32.RegistryKey regKey, string subKey, string name, string value)
        {
            try
            {
                using (regKey = regKey.CreateSubKey(subKey, RegistryKeyPermissionCheck.ReadWriteSubTree))
                {
                    regKey.SetValue(name, value);
                    regKey.Flush();
                    regKey.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                ComLog.WriteLog(ComLog.Level.Exception
                              , System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "."
                              + System.Reflection.MethodBase.GetCurrentMethod().Name + "()"
                              , ex.Message); 
                return false;
            }
        }
        /// <summary>
        /// 레지스트리에 값 쓰기
        /// </summary>
        /// <param name="regKey"></param>
        /// <param name="subKey"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool SetValue(Microsoft.Win32.RegistryKey regKey, string subKey, string name, long value)
        { 
            try
            {
                using (regKey = regKey.CreateSubKey(subKey, RegistryKeyPermissionCheck.ReadWriteSubTree))
                {
                    regKey.SetValue(name, value, RegistryValueKind.DWord);
                    regKey.Flush();
                    regKey.Close();
                }
                return true;
            }
            catch (Exception ex)
            {
                ComLog.WriteLog(ComLog.Level.Exception
                              , System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "."
                              + System.Reflection.MethodBase.GetCurrentMethod().Name + "()"
                              , ex.Message); 
                return false;
            }
        }

        public static bool IsExistKey(Microsoft.Win32.RegistryKey regKey, string subKey)
        {
            bool retValue = false;
            try
            {
                using (Microsoft.Win32.RegistryKey key = regKey.OpenSubKey(subKey))
                {
                    if (key != null) retValue = true;
                }
            }
            catch (Exception ex)
            {
                ComLog.WriteLog(ComLog.Level.Exception
                              , System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name + "."
                              + System.Reflection.MethodBase.GetCurrentMethod().Name + "()"
                              , ex.Message);
                retValue = false;
            }
            return retValue;
        }
    }
}
