1º fazer a classe AppConfig.cs

public enum   ConfigFileType
{
 WebConfig ,
 AppConfig
}

<span id="more-17"></span>

public class AppConfig : System.Configuration.AppSettingsReader
{
 public string  docName = String.Empty;
 private  XmlNode node=null;
    private int _configType;

 public   int ConfigType
 {
  get
  {
   return _configType;
  }
  set
  {
   _configType=value;
  }
 }

 public bool SetValue(string key, string value)
 {
  XmlDocument cfgDoc = new XmlDocument();
  loadConfigDoc(cfgDoc);
   // retrieve the appSettings node
        node = cfgDoc.SelectSingleNode(&quot;//connectionStrings&quot;);

   if( node == null )
   {
                throw new System.InvalidOperationException(&quot;connectionStrings section not found&quot;);
   }

  try
  {
   // XPath select setting &quot;add&quot; element that contains this key
   XmlElement addElem= (XmlElement)node.SelectSingleNode(&quot;//add[@name='&quot; +key +&quot;']&quot;) ;
   if (addElem!=null)
   {
    addElem.SetAttribute(&quot;value&quot;,value);
   }
    // not found, so we need to add the element, key and value
   else
   {
    XmlElement entry = cfgDoc.CreateElement(&quot;add&quot;);
    entry.SetAttribute(&quot;name&quot;,key);
                entry.SetAttribute(&quot;connectionString&quot;, value);
    node.AppendChild(entry);
   }
   //save it
   saveConfigDoc(cfgDoc,docName);
   return true;
  }
  catch
  {
   return false;
  }
 }

 private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath)
 {
  try
  {
   XmlTextWriter writer = new XmlTextWriter( cfgDocPath , null );
   writer.Formatting = Formatting.Indented;
   cfgDoc.WriteTo( writer );
   writer.Flush();
   writer.Close();
   return;
  }
  catch
  {
   throw;
  }
 }

 public bool removeElement ( string elementKey)
 {
  try
  {
   XmlDocument cfgDoc = new XmlDocument();
   loadConfigDoc(cfgDoc);
   // retrieve the appSettings node
            node = cfgDoc.SelectSingleNode(&quot;//connectionStrings&quot;);
   if( node == null )
   {
                throw new System.InvalidOperationException(&quot;connectionStrings section not found&quot;);
   }
   // XPath select setting &quot;add&quot; element that contains this key to remove
   node.RemoveChild( node.SelectSingleNode(&quot;//add[@name='&quot; +elementKey +&quot;']&quot;) );

   saveConfigDoc(cfgDoc,docName);
   return true;
  }
  catch
  {
   return false;
  }
 }

 private XmlDocument loadConfigDoc( XmlDocument cfgDoc )
 {
  // load the config file
        Console.WriteLine(Convert.ToInt32(ConfigFileType.AppConfig)+&quot;___&quot;+Convert.ToInt32(ConfigType));

  if(  Convert.ToInt32(ConfigType)==0)
  {

   docName= ((Assembly.GetEntryAssembly()).GetName()).Name;
   docName +=   &quot;.exe.config&quot;;
  }
  else
  {
   docName=System.Web.HttpContext.Current.Server.MapPath(&quot;web.config&quot;);
  }
  cfgDoc.Load( docName );
  return cfgDoc;
 }

}

2º Na hora de usar

AppConfig config = new AppConfig();
            config.removeElement("localhostcs");
            config.SetValue("QUAL KEY QUER MUDAR?", "O QUE QUER ADD?");

ps. o codigo da classe Appconfig.cs esta pronto para mudar a connectio string
portanto prestar atenção na hora de mudar… mude para aquilo que vc quer mudar no seu app.config =D

flw

Posts Relacionados