Ağu 25 2010

appSettings e Programatik key ekleme,düzenleme,silme

Category: c#by_derkan @ 22:24
yazdığım,derlediğim işe yarar statik method,fonksiyonları paylaşıyorum.bir yerden başlamak lazım.

using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration; // gerekmeyebilir

yoksa ekliyoruz.
 
 // appSettings e erişim


      public static string GetAppKey(string key) // varsa değeri yoksa null döndürür
        {
            return WebConfigurationManager.AppSettings[key];
        }

        public static int GetKeyInt(string key) // varsa değeri yoksa 0 döndürür
        {
            int result;
            Int32.TryParse(WebConfigurationManager.AppSettings[key], out result);
            return result;
        }

        public static bool GetKeyBool(string key) // True/False değer kontrolü yapar.
        {
            bool sonuc;
            bool.TryParse(GetAppKey(key),out sonuc);
            return sonuc;
        }

// düzenleme

        public static bool SetAppValue(string anahtar, string deger) // ayar varsa günceller yoksa ekler :=)
        {
            try
            {
                Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
                AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
 
                if (GetAppKey(anahtar) == null)
                {
                    appSettingsSection.Settings.Add(anahtar, deger);
                    configuration.Save();
 
                }
                else
                {
                    appSettingsSection.Settings[anahtar].Value = deger;
                    configuration.Save();
                }
 
                return true;
            }
            catch
            {
                return false;
            }
        }

// kaldırma

        public static bool RemoveAppKey(string key) // keyi web.config den kaldırır
        {
            try
            {
                Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
                AppSettingsSection appSettingsSection = (AppSettingsSection)configuration.GetSection("appSettings");
 
                if (GetAppKey(key) != null)
                {
                    appSettingsSection.Settings.Remove(key);
                    configuration.Save();
                }
 
                return true;
            }
            catch
            {
                return false;
            }
        }


Tags:

Yorumlar

1.
kız oyunları kız oyunları Türkiye says:

Bununla ilgili çok araştırma yaptım. Ayrıca bu konuyla ilgili farklı bilgilerimde var sizlerle fırsat olursa paylaşmak isterim.  Güzel bir paylaşım olmuş. Bu tarz paylaşımlar sevindirici.

İyi çalışmalar..

2.
boğaz turu boğaz turu Türkiye says:

teşekkürler paylaşım için

Yorum ekle


(Gravatar simgesini gösterecek)

  Country flag

biuquote
  • Yorum
  • Canlı önizleme
Loading