Click here to Skip to main content
15,891,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm being trying to setup a custom section in my app.config for a COM object I'm building with no luck, after spending a couple days; yes a couple of days; looking at sample code explanations from different forums I still cannot make it work. This is a simplified code I put together to describe the issue:

ConfigurationTest.cs
C#
using System.Runtime.InteropServices;
using System.Configuration;

namespace configurationTest
{
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [ProgId("Utils.ConfigurationTest")]
    public class ConfigurationTest
    {
        public string passWord = "";

        public ConfigurationTest()
        {

        }

        public string getPassword()
        {
            configurationHelper config = (configurationHelper)ConfigurationManager.GetSection("credentials");
            this.passWord = config.PassWord;
            return this.passWord.ToString();
        }
    }
}

ConfigurationHelper.cs
C#
using System.Configuration;

namespace configurationTest
{
    public class configurationHelper : System.Configuration.ConfigurationSection
    {
        [ConfigurationProperty("passWord")]
        public string PassWord
        {
            get { return (string)this["passWord"]; }
            set { this["passWord"] = value; }
        }
    }
}


app.Config
C#
<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <section name="credentials" type="configurationTest.configurationHelper, ConfigurationHelper"/>
  </configSections>

  <credentials passWord ="PDm!@M!11"/>

</configuration>


My problem is when I instantiate the COM object and call the getPassword method the variable "config" is null. What am I doing wrong? any ideas are welcome,

Thank you
Posted

Why do you try it so complicately? What about:
XML
<?xml version="1.0" encoding ="utf-8"?>
<configuration>
    <appSettings>
        <add key="passWord" value="abc123"/>
    </appSettings>
</configuration>

and in your code:
C#
this.passWord = System.Configuration.ConfigurationManager.AppSettings["passWord"];
 
Share this answer
 
Comments
lenniscata 7-Oct-13 9:19am    
Hi Bernhnard, thank you for taking the time to look at this issue, I did what you suggested and still getting the same results; System.Configuration.ConfigurationManager.AppSettings["passWord"]
returns null. I tested this code in both Windows 8 and Windows 7 with VS2010.

Luis
S. M. Ahasan Habib 7-Oct-13 13:46pm    
web.config/app.config keys are case sensitive. Please check that. Other then that you should retrive config value with the above sample code.
lenniscata 7-Oct-13 19:44pm    
Thank you Ahasan, I'm aware of it; as I mentioned in other comment the problem relies in the COM object, see the link I posted.
It turns out that the problem is the COM object, COM object don't use config files,
see this posting

http://preview.tinyurl.com/ofjqcbd[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900