Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / February 2007

Tip: Looking for answers? Try searching our database.

Writing to an external config file.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Martin Hart - 15 Feb 2007 17:54 GMT
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!
}
Oliver Sturm - 15 Feb 2007 20:13 GMT
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
Signature

http://www.sturmnet.org/blog

Martin Hart - 16 Feb 2007 08:20 GMT
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,
>
[quoted text clipped - 19 lines]
>
>                Oliver Sturm
Oliver Sturm - 16 Feb 2007 19:58 GMT
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
Signature

http://www.sturmnet.org/blog

Martin Hart - 19 Feb 2007 17:58 GMT
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,
>
[quoted text clipped - 5 lines]
>
>                Oliver Sturm

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.