程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 重寫GetSetting SaveSetting

C# 重寫GetSetting SaveSetting

編輯:C#入門知識

[csharp] 
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 www.2cto.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; 
        } 
   } 
      

作者:vbvcde

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved