Foros del Web » Programación para mayores de 30 ;) » .NET »

C# -> App.config no puedo modificarlo desde el programa :S

Estas en el tema de C# -> App.config no puedo modificarlo desde el programa :S en el foro de .NET en Foros del Web. bien, mi programa tiene esta parte Cita: ConfigurationManager.AppSettings.Set("AceptoTocs", "true"); pero no modifica el archivo, sige en false aunque haga esto, a que puede deverse? tengo ...
  #1 (permalink)  
Antiguo 25/07/2008, 05:50
 
Fecha de Ingreso: diciembre-2007
Mensajes: 107
Antigüedad: 17 años, 2 meses
Puntos: 4
C# -> App.config no puedo modificarlo desde el programa :S

bien, mi programa tiene esta parte
Cita:
ConfigurationManager.AppSettings.Set("AceptoTocs", "true");
pero no modifica el archivo, sige en false aunque haga esto, a que puede deverse?

tengo puesto el
Cita:
using System.Configuration;
y en references system.configuration

mi appconfig es este:
Código:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

    <appSettings>
      <add key="AceptoTocs" value="false"/>
    </appSettings>

</configuration>
  #2 (permalink)  
Antiguo 25/07/2008, 06:47
Avatar de Peterpay
Colaborador
 
Fecha de Ingreso: septiembre-2007
Ubicación: San Francisco, United States
Mensajes: 3.858
Antigüedad: 17 años, 6 meses
Puntos: 87
Respuesta: C# -> App.config no puedo modificarlo desde el programa :S

Sorry ayer se me paso ese dato

revisa esto

http://www.codeproject.com/KB/cs/modconfigruntime.aspx
__________________
Curso WF4
http://cursos.gurudotnet.com/ DF
Aprende HTML5
  #3 (permalink)  
Antiguo 25/07/2008, 07:04
 
Fecha de Ingreso: diciembre-2007
Mensajes: 107
Antigüedad: 17 años, 2 meses
Puntos: 4
Respuesta: C# -> App.config no puedo modificarlo desde el programa :S

disculpa como inserto este código en un boton?
Cita:
// Updates a key within the App.config

public void UpdateKey(string strKey, string newValue)
{
if (!KeyExists(strKey))
throw new ArgumentNullException("Key", "<" + strKey +
"> does not exist in the configuration. Update failed.");
XmlNode appSettingsNode =
xmlDoc.SelectSingleNode("configuration/appSettings");
// Attempt to locate the requested setting.

foreach (XmlNode childNode in appSettingsNode)
{
if (childNode.Attributes["key"].Value == strKey)
childNode.Attributes["value"].Value = newValue;
}
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory +
"..\\..\\App.config");
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformati on.ConfigurationFile);
}
  #4 (permalink)  
Antiguo 25/07/2008, 07:08
Avatar de Peterpay
Colaborador
 
Fecha de Ingreso: septiembre-2007
Ubicación: San Francisco, United States
Mensajes: 3.858
Antigüedad: 17 años, 6 meses
Puntos: 87
Respuesta: C# -> App.config no puedo modificarlo desde el programa :S

puedes declararlo en el mismo form y solo en el

btn_Click(object sender, eventargs e)
{
this.UpdateKey("AceptoTocs","true");
}
__________________
Curso WF4
http://cursos.gurudotnet.com/ DF
Aprende HTML5
  #5 (permalink)  
Antiguo 25/07/2008, 07:23
 
Fecha de Ingreso: diciembre-2007
Mensajes: 107
Antigüedad: 17 años, 2 meses
Puntos: 4
Respuesta: C# -> App.config no puedo modificarlo desde el programa :S

da error, mi codigo final es este:

Cita:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;


namespace DivineRO_Suite_Server
{
public partial class Terminos_Legales : Form
{
public Terminos_Legales()
{
InitializeComponent();
}

public void UpdateKey(string strKey, string newValue)
{
if (!KeyExists(strKey))
throw new ArgumentNullException("Key", "<" + strKey +
"> does not exist in the configuration. Update failed.");
XmlNode appSettingsNode =
xmlDoc.SelectSingleNode("configuration/appSettings");
// Attempt to locate the requested setting.

foreach (XmlNode childNode in appSettingsNode)
{
if (childNode.Attributes["key"].Value == strKey)
childNode.Attributes["value"].Value = newValue;
}
xmlDoc.Save(AppDomain.CurrentDomain.BaseDirectory +
"..\\..\\App.config");
xmlDoc.Save(AppDomain.CurrentDomain.SetupInformati on.ConfigurationFile);
}

private void bacepto_Click(object sender, EventArgs e)
{
this.UpdateKey("AceptoTocs", "true");
Inicio inicioform = new Inicio();
inicioform.Show();
this.Close();
}
}
}
que puede ser?
  #6 (permalink)  
Antiguo 26/09/2008, 09:20
 
Fecha de Ingreso: noviembre-2004
Mensajes: 1
Antigüedad: 20 años, 3 meses
Puntos: 0
Respuesta: C# -> App.config no puedo modificarlo desde el programa :S

Change values as XML file


public void editConfig(string key, string value)
{
XmlDocument Config = new XmlDocument();
string path = "Here your .Config File path";
Config.Load(path);

XmlNodeList apps = Config.GetElementsByTagName("appSettings");
XmlNodeList list = ((XmlElement)apps[0]).GetElementsByTagName("add");
foreach (XmlElement nodo in list)
{
if (nodo.GetAttribute("key") == key)
{
nodo.SetAttribute("value",value);
break;
}
}
Config.Save(path);
}

Última edición por Ralstlin; 26/09/2008 a las 09:27
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:39.