PC Review


Reply
Thread Tools Rate Thread

Writing to an external config file.

 
 
Martin Hart
Guest
Posts: n/a
 
      15th Feb 2007
Hi:

I am having trouble writing to an external .config file (a .config file
from another application, not the running application)

Below I have included the code I am using, but although no exception or
error is thrown, noting is written to the file! All the sections and
elements exist in the .config file and the debugger passes through each
scenario.

Can anyone please help me?

TIA,
Martin.

Code
****


private void saveToExternalConfigFile()
{
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = @"MovilGesNet.exe.config";
Configuration config =
ConfigurationManager.OpenMappedExeConfiguration(fileMap,
ConfigurationUserLevel.None);
ConfigurationSectionGroup seccion =
config.GetSectionGroup("applicationSettings");
if(seccion != null)
{
ClientSettingsSection secPropertiesSettings =
seccion.Sections["MovilGesNet.Properties.Settings"] as
ClientSettingsSection;
if(secPropertiesSettings != null)
{
foreach(SettingElement element in secPropertiesSettings.Settings)
{
switch(element.Name)
{
case "Host":
element.Value.ValueXml.InnerText = "MyNewAppText";
break;
}
}
}
}
seccion = config.GetSectionGroup("userSettings");
if(seccion != null)
{
ClientSettingsSection secPropertiesSettings =
seccion.Sections["MovilGesNet.Properties.Settings"] as
ClientSettingsSection;
if(secPropertiesSettings != null)
{
foreach(SettingElement element in secPropertiesSettings.Settings)
{
switch(element.Name)
{
case "PathPdaImport":
element.Value.ValueXml.InnerText = "MyNewUserText";
break;
}
}
}
}
config.Save(ConfigurationSaveMode.Full); // Not doing anything!
}
 
Reply With Quote
 
 
 
 
Oliver Sturm
Guest
Posts: n/a
 
      15th Feb 2007
Hello Martin,

I've only had a quick look at your code, but:

> ConfigurationSectionGroup seccion = config.GetSectionGroup("applicationSettings");
> if(seccion != null)
> {
> ClientSettingsSection secPropertiesSettings = seccion.Sections["MovilGesNet.Properties.Settings"] as ClientSettingsSection;


I'm not sure if this results in anything. I think the section might be of
type DefaultSection under these circumstances. Are you sure the following
block of code is actually executed in your app? Maybe you're not really
making any changes after all.

> if(secPropertiesSettings != null)
> {
> ...
> }



Oliver Sturm
--
http://www.sturmnet.org/blog
 
Reply With Quote
 
Martin Hart
Guest
Posts: n/a
 
      16th Feb 2007
Oliver:

Firstly, nice to see you here :-)

Secondly, I have traced the code execution and I do hit the lines that
actually make the changes:
e.g.
element.Value.ValueXml.InnerText = "MyNewAppText";

If I ask for the value of the element, it shows me the new value, so it
is changing in memory, it's just the .Save that does nothing - no error,
no exception - no save!!

Any further ideas would be gratefully accepted :-))

Regards,
Martin.


Oliver Sturm escribió:
> Hello Martin,
>
> I've only had a quick look at your code, but:
>
>> ConfigurationSectionGroup seccion =
>> config.GetSectionGroup("applicationSettings");
>> if(seccion != null)
>> {
>> ClientSettingsSection secPropertiesSettings =
>> seccion.Sections["MovilGesNet.Properties.Settings"] as
>> ClientSettingsSection;

>
> I'm not sure if this results in anything. I think the section might be
> of type DefaultSection under these circumstances. Are you sure the
> following block of code is actually executed in your app? Maybe you're
> not really making any changes after all.
>
>> if(secPropertiesSettings != null)
>> {
>> ...
>> }

>
>
> Oliver Sturm

 
Reply With Quote
 
Oliver Sturm
Guest
Posts: n/a
 
      16th Feb 2007
Hello Martin,

>Any further ideas would be gratefully accepted :-))


Sorry, I don't have any for the time being... If you want to, feel free to
send me a program that reproduces your issue and I'll have a look to see
whether I can help you.


Oliver Sturm
--
http://www.sturmnet.org/blog
 
Reply With Quote
 
Martin Hart
Guest
Posts: n/a
 
      19th Feb 2007
Oliver:

Okay, I solved it. There is a 'secret' property that must be set before
saving is enabled.

The code is:
seccion.SectionInformation.ForceSave = true;

It not enough to just change the ValueXml, you must also force the save.

Thanks for your help, see you in the DevEx forums!!!

Regards,
Martin.

Oliver Sturm escribió:
> Hello Martin,
>
>> Any further ideas would be gratefully accepted :-))

>
> Sorry, I don't have any for the time being... If you want to, feel free
> to send me a program that reproduces your issue and I'll have a look to
> see whether I can help you.
>
>
> Oliver Sturm

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why this web config cannot reference an external config file? dh Microsoft C# .NET 0 2nd Oct 2009 01:12 AM
writing to config file =?Utf-8?B?Vmlua2k=?= Microsoft ASP .NET 3 21st Nov 2007 04:45 PM
How to reference an external config file in app.config =?Utf-8?B?QmVuamFtaW4=?= Microsoft Dot NET Framework 4 14th Aug 2007 04:46 AM
.config file - writing to it? UJ Microsoft Dot NET 4 26th Oct 2005 12:22 PM
Writing to config file Gav Microsoft C# .NET 1 9th Jun 2005 02:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:13 PM.