Save Configuration dynamically without lost comments

  • Thread starter Alhambra Eidos Desarrollo
  • Start date

A

Alhambra Eidos Desarrollo

Hi, thanks, mister

The code

string rutaConfig = tbRutaConfigServicioBase.Text;

'// Map to the application configuration file.

ExeConfigurationFileMap configFile = New ExeConfigurationFileMap();

configFile.ExeConfigFilename = rutaConfig;

Dim config As Configuration =
ConfigurationManager.OpenMappedExeConfiguration(configFile,
ConfigurationUserLevel.None);

string[] keys = config.AppSettings.Settings.AllKeys;


KeyValueConfigurationCollection settings1 = config.AppSettings.Settings();

settings1("GRUPOBACKUP.Ruta_Grabar_Ficheros").Value =
tbGRUPOBACKUP_Ruta_Grabar_Ficheros.Text;
settings1("SERVIDOR_INDEXADO").Value = tbSERVIDOR_INDEXADO.Text;

settings1("PDF_CREADOR").Value = tbPDF_CREADOR.Text;

settings1("PDF_AUTOR").Value = tbPDF_AUTOR.Text;

settings1("PDF_TITULO").Value = tbPDF_TITULO.Text;

settings1("PDF_ASUNTO").Value = tbPDF_ASUNTO.Text;


config.ConnectionStrings.ConnectionStrings["SQLConnection"].ConnectionString
= tbCadenaConexionFirmasParaServicioBase.Text;

config.ConnectionStrings.ConnectionStrings["SQLConnectionSecundaria"].ConnectionString = tbCadenaConexionOGFParaServicioBase.Text;

' Modifica el app.config pero pierde los comentarios del appsettings

' Así se modifica el app.config pero añade secciones vacías, modifica
bastante la estructura. Además al utilizar EnterpriseLibrary se producen
errores de ensamblados

config.Save(ConfigurationSaveMode.Modified, True); ' <==== Fails here !!!


The error:

System.Configuration.ConfigurationErrorsException was unhandled
BareMessage="Se ha producido un error al crear el controlador de sección
de configuración para exceptionHandling: No se puede cargar el archivo o
ensamblado 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' ni una de sus
dependencias. El sistema no puede hallar el archivo especificado."
Filename="C:\Archivos de programa\GRUPO BACKUP\GRUPO BACKUP Servicios
Principales OGF\ServicioBase.exe.config"
Line=5
Message="Se ha producido un error al crear el controlador de sección de
configuración para exceptionHandling: No se puede cargar el archivo o
ensamblado 'Microsoft.Practices.EnterpriseLibrary.ExceptionHandling,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=null' ni una de sus
dependencias. El sistema no puede hallar el archivo especificado.
(C:\Archivos de programa\GRUPO BACKUP\GRUPO BACKUP Servicios Principales
OGF\ServicioBase.exe.config line 5)"
Source="System.Configuration"
StackTrace:
en
System.Configuration.BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)
en
System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String
configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject,
Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
en System.Configuration.BaseConfigurationRecord.GetSection(String
configKey, Boolean getLkg, Boolean checkPermission)
en System.Configuration.ConfigurationSectionCollection.Get(String name)
en
System.Configuration.ConfigurationSectionCollection.<GetEnumerator>d__0.MoveNext()
en
System.Configuration.Configuration.ForceGroupsRecursive(ConfigurationSectionGroup group)
en System.Configuration.Configuration.SaveAsImpl(String filename,
ConfigurationSaveMode saveMode, Boolean forceSaveAll)
en System.Configuration.Configuration.Save(ConfigurationSaveMode
saveMode, Boolean forceSaveAll)
en
GRUPOBACKUP.InstaladorConfiguradorOGF.Cliente.Util.FPrincipal.bGuardarConfiguracionServicioBase_Click(Object
sender, EventArgs e) en
D:\DESA\ExpedienteElectronico\Instalacion\InstaladorConfigurador\GRUPOBACKUP.InstaladorConfiguradorOGF.Cliente.Util\FPrincipal.vb:línea 231
en System.Windows.Forms.Control.OnClick(EventArgs e)
en System.Windows.Forms.Button.OnClick(EventArgs e)
en System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
en System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons
button, Int32 clicks)
en System.Windows.Forms.Control.WndProc(Message& m)
en System.Windows.Forms.ButtonBase.WndProc(Message& m)
en System.Windows.Forms.Button.WndProc(Message& m)
en
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
en System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,
Int32 msg, IntPtr wparam, IntPtr lparam)
en System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
en
System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32
dwComponentID, Int32 reason, Int32 pvLoopData)
en
System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
reason, ApplicationContext context)
en
System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
en System.Windows.Forms.Application.Run(Form mainForm)
en GRUPOBACKUP.InstaladorConfiguradorOGF.Program.Main() en
D:\DESA\ExpedienteElectronico\Instalacion\InstaladorConfigurador\GRUPOBACKUP.InstaladorConfiguradorOGF\Program.cs:línea 19
en System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
en System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
en Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
en System.Threading.ThreadHelper.ThreadStart_Context(Object state)
en System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
en System.Threading.ThreadHelper.ThreadStart()


line 5 in app.config
<section name="exceptionHandling"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null" />


The app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="loggingConfiguration"
type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings,
Microsoft.Practices.EnterpriseLibrary.Logging, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null" />
<section name="exceptionHandling"
type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings,
Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=null" />
</configSections>
<appSettings>
....


IMPORTANT: If copy assemblies of EnterpriseLibrary where I have my project
output, all works fine.
Thanks in advance.
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top