7 Mart 2016 Pazartesi

ConfigurationManager

app.config dosyası
app.config dosyası projeye eklenir. Dosya şuna benzer. key/value çiftleri şeklindedir.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="countoffiles" value="7" />
    <add key="logfilelocation" value="abc.txt" />
  </appSettings>
</configuration>
webconfig dosyası
Dosya şuna benzer.
<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.0"/>
    </system.web>
    <connectionStrings>
      <add name="#" connectionString="#" providerName="..."/>
    <connectionStrings>
  </configuration>

ConfigurationManager sınıfı
Bu sınıfı kullanmak için şu satırı dahil ederiz.
using System.Configuration;
AppSetting Alanı
app.config dosyasını okumak için ConfigurationManager sınıfı kullanılır. app.config dosyası host application içinde olmalıdır. DLL projesi içindeyse otomatik olarak okunmaz!

ConfigurationManager sınıfınının kendisi ve içindeki her Property static'tir.

Bu sınıf iki tane static Property sunar. Bunlar AppSettings ve ConnectionStrings nesneleridir. AppSettings app.config ile kullanılır. ConnectionStrings ise webconfig ile kullanılır.

string configvalue1 = ConfigurationManager.AppSettings["countoffiles"];
string configvalue2 = ConfigurationManager.AppSettings["logfilelocation"];






Hiç yorum yok:

Yorum Gönder