Link to home
Create AccountLog in
Avatar of Anjeneya Murthy
Anjeneya MurthyFlag for India

asked on

Need help in using System.Configuration collections

Hi,
I am trying to parse the application configuration file. I am required to use the System.Configuration in the code to access the sections.
The configuration goes as below
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="CopyFiles" type="ConfigManagerTest.Config.CopyFilesSection, ConfigManagerTest"/>
  </configSections>

  
  <CopyFiles><!--Section-->
    
    <CopyFile><!--CopyFileCollection : ConfigElementCollection-->
      <Destination path="Location1"/><!--DestinationPath: ConfigElement-->
      <Files><!-- FilesCollection: ConfigElementCollection-->
        <clear/>
        <add FileName=""/>
        <add FileName=""/>
      </Files>
    </CopyFile>

    <CopyFile>
      <Destination path="Location2"/>
      <Files>
        <clear/>
        <add FileName=""/>
        <add FileName=""/>
      </Files>
    </CopyFile>

    <CopyFile>
      <Destination path="Location3"/>
      <Files>
        <clear/>
        <add FileName=""/>
        <add FileName=""/>
      </Files>
    </CopyFile>
  
  </CopyFiles>
  
</configuration>

Open in new window


Q1. Is it possible to use configuration classes to achive the purpose shown above?

I have created separate classes for this. Please review and let me know where I am going wrong!
I am not good at using Configuration classes. My guess is "probably we cannot use the properties like Destination and Files collection in CopyFilesCollection"
Please help. I have done my best and now turned for help.
I dont know how I can use "add" for CopyFile in the app config file.
The complete code is attached in the .zip file
To-ExpertsExchange.zip
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Yes it is possible. You could achieve using the code looks like given below.

Please do refer the links
http://www.codeproject.com/Articles/32490/Custom-Configuration-Sections-for-Lazy-Coders
http://msdn.microsoft.com/en-us/library/2tw134k3(v=vs.100).aspx
http://blogs.msdn.com/b/aspnetue/archive/2008/10/02/system-configuration-configurationmanager-source-c.aspx
http://www.codeproject.com/Articles/163081/Custom-Configuration-Sections-in-NET



//Read config file
Configuration config = 
                ConfigurationManager.OpenExeConfiguration(path);
  
//Add custom section
config.Sections.Add("MySection", instance);

//Save the modified file.
config.Save(ConfigurationSaveMode.Modified);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Anjeneya Murthy
Anjeneya Murthy
Flag of India image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Anjeneya Murthy

ASKER

Found the answer myself. Will update here as soon as possible