C# 重写 GetSetting SaveSetting

using Microsoft.Win32;
using System;
using System.Collections;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Windows.Forms;
/* 2012.6.21
 * 注册表读写配置类 V1.1
 * By:Transmart gongxd@126.com
 * 我们提供软件外包服务,专注于工控组态软硬件开发,涉足网路、数据库、系统编程
 * 欢迎大家来信交流
 * 转载时请保留这段声明,谢谢
 */

namespace Main
{
   public class Class_Setting
    {
       static string myAppName=null ;

       private static bool IsNothing(object Expression)
        {
            return (Expression == null);
        }
        private static string FormRegKey(string sApp, string sSect)
        {
            if (IsNothing(sApp) || (sApp.Length == 0))
            {
                return @"Software\Transmart";
            }
            if (IsNothing(sSect) || (sSect.Length == 0))
            {
                return (@"Software\Transmart\" + sApp);
            }
            return (@"Software\Transmart\" + sApp + @"\" + sSect);
        }
        private static void CheckPathTransmart(string s)
        {
            if ((s == null) || (s.Length == 0))
            {
                throw new ArgumentException("参数路径为空!");
            }
        }
       /// <summary>
       /// 初始化程序名,使用时请先初始化程序名
       /// </summary>
       /// <param name="Appname"></param>
        public static void IniSetting(string Appname)
        {
            myAppName = Appname;
        }

        public static void SaveSetting(string Section, string Key, string Setting)
        {
            SaveSetting(myAppName, Section, Key, Setting);
        }

        public static void SaveSetting(string AppName, string Section, string Key, string Setting)
        {
            CheckPathTransmart(AppName);
            CheckPathTransmart(Section);
            CheckPathTransmart(Key);
            string subkey = FormRegKey(AppName, Section);
            RegistryKey key = Registry.LocalMachine.CreateSubKey(subkey);
            if (key == null)
            {
                throw new ArgumentException("无法创建配置!");
            }
            try
            {
                key.SetValue(Key, Setting);
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                key.Close();
            }
        }

        public static string GetSetting( string Section, string Key, string Default = "")
        {
            return GetSetting(myAppName, Section, Key, Default);
        }

        public static string GetSetting(string AppName, string Section, string Key, string Default = "")
        {
            object obj2;
            RegistryKey key = null;
            CheckPathTransmart(AppName);
            CheckPathTransmart(Section);
            CheckPathTransmart(Key);
            if (Default == null)
            {
                Default = "";
            }
            string name = FormRegKey(AppName, Section);
            try
            {
                key = Registry.LocalMachine.OpenSubKey(name);
                if (key == null)
                {
                    return Default;
                }
                obj2 = key.GetValue(Key, Default);
            }
            finally
            {
                if (key != null)
                {
                    key.Close();
                }
            }
            if (obj2 == null)
            {
                return null;
            }
            if (!(obj2 is string))
            {
                throw new ArgumentException("参数错误!");
            }
            return (string) obj2;
        }



        public static string[,] GetAllSettings(string Section)
        {
            return GetAllSettings(  myAppName,   Section);
        }
       public static string[,] GetAllSettings(string AppName, string Section)
        {
            CheckPathTransmart(AppName);
            CheckPathTransmart(Section);
            string name = FormRegKey(AppName, Section);
            RegistryKey key = Registry.CurrentUser.OpenSubKey(name);
            if (key == null)
            {
                return null;
            }
            string[,] strArray = null;
            try
            {
                if (key.ValueCount == 0)
                {
                    return strArray;
                }
                string[] valueNames = key.GetValueNames();
                int upperBound = valueNames.GetUpperBound(0);
                string[,] strArray3 = new string[upperBound + 1, 2];
                int num3 = upperBound;
                for (int i = 0; i <= num3; i++)
                {
                    string str2 = valueNames[i];
                    strArray3[i, 0] = str2;
                    object obj2 = key.GetValue(str2);
                    if ((obj2 != null) && (obj2 is string))
                    {
                        strArray3[i, 1] = obj2.ToString();
                    }
                }
                strArray = strArray3;
            }
            catch (StackOverflowException exception)
            {
                throw exception;
            }
            catch (OutOfMemoryException exception2)
            {
                throw exception2;
            }
            catch (ThreadAbortException exception3)
            {
                throw exception3;
            }
            catch (Exception)
            {
            }
            finally
            {
                key.Close();
            }
            return strArray;
        }
   }
     
}



类似VB里面的  GetSetting   SaveSetting ,非常方便的存几个参数,也不用带vb的运行库了

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值