0

I am getting following exception when I run the code:

FileNotFoundException: Could not load file or assembly 'My' or one of its dependencies. The system cannot find the file specified.

Following is my app.config:

<configuration>
  <configSections>
    <section name="registerCompanies" type="My.MyConfigSection, My" />
  </configSections>
  <registerCompanies>
    <add name="Tata Motors" code="Tata"/>
    <add name="Honda Motors" code="Honda"/>
  </registerCompanies>
</configuration>

Following is my namespace and class:

using System.Configuration;
using System.Linq;

namespace My
{
    public class MyConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
        public MyConfigInstanceCollection Instances
        {
            get { return (MyConfigInstanceCollection)this[""]; }
            set { this[""] = value; }
        }
    }
    public class MyConfigInstanceCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new MyConfigInstanceElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            //set to whatever Element Property you want to use for a key
            return ((MyConfigInstanceElement)element).Name;
        }

        public new MyConfigInstanceElement this[string elementName]
        {
            get
            {
                return this.OfType<MyConfigInstanceElement>().FirstOrDefault(item => item.Name == elementName);
            }
        }
    }

    public class MyConfigInstanceElement : ConfigurationElement
    {
        //Make sure to set IsKey=true for property exposed as the GetElementKey above
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)base["name"]; }
            set { base["name"] = value; }
        }

        [ConfigurationProperty("code", IsRequired = true)]
        public string Code
        {
            get { return (string)base["code"]; }
            set { base["code"] = value; }
        }
    }
}

I am trying to retrieve the app.config using the below code:

MyConfigSection config =  ConfigurationManager.GetSection("registerCompanies") as MyConfigSection;
Console.WriteLine(config.Instances["Honda Motors"].Code);
foreach (MyConfigInstanceElement e in config.Instances)
{
   Console.WriteLine("Name: {0}, Code: {1}", e.Name, e.Code);
}

The only reason I think it's not working is may be because my namespace My is in another .cs file ConfigSetup.cs and I am trying to retrieve the appconfig in another .cs file.

I am not sure how to address in the below line that My.MyCofigSection exists in ConfigSetup.cs and the project name is FileManager if I need to include that as well ? Below line gives me an exception that I mentioned above.

<section name="registerCompanies" type="My.MyConfigSection, My" />
DevOpS_PRO
  • 41
  • 1
  • 5
  • In this declaration `type="My.MyConfigSection, My"` you are saying that there is a class called `My.MyConfigSection` implemented in an assembly named `My.dll`. I'm guessing you have the assembly name wrong. The portion after the comma is the assembly name, not the namespace. – John Wu Sep 23 '21 at 00:53
  • Does this answer your question? [Could not load file or assembly or one of its dependencies](https://stackoverflow.com/questions/4469929/could-not-load-file-or-assembly-or-one-of-its-dependencies) – Ken White Sep 23 '21 at 00:53
  • Hi John Wu I have changed `type="My.MyConfigSection, My"` to `type="My.MyConfigSection, ConfigSetup"` however I still get the same error – DevOpS_PRO Sep 23 '21 at 00:57
  • 1
    Hi John Wu I have changed `type="My.MyConfigSection, My"` to `type="My.MyConfigSection, File_Manager"` and it worked thank you very much – DevOpS_PRO Sep 23 '21 at 01:13
  • If it works, you can self-answer and accept it to help new people to find it next time ! – Elikill58 Sep 30 '21 at 08:25

1 Answers1

0

I have changed type="My.MyConfigSection, My" to type="My.MyConfigSection, File_Manager" and it worked thank you very much

DevOpS_PRO
  • 41
  • 1
  • 5