Random Musings

June 12, 2009

Using App.Config in C# dll library

Filed under: Dynamics CRM — haditeo @ 5:03 pm
Tags: ,

Here are the steps to use App.Config in dll;
1) Make sure that the build action of the App.Config is ‘Content’ and Copy to Output Directory = ‘Copy always’
2) Make sure that the App.Config file is placed in the \bin\Debug folder
3) Read the App.Config by using XmlDocument.Load() method

            XmlDocument doc = new XmlDocument();
            doc.Load(AppDomain.CurrentDomain.BaseDirectory + "\\" + "App.Config");

            XmlNode appSettingsNode = doc.GetElementsByTagName("appSettings")[0];

            foreach(XmlNode node in appSettingsNode.ChildNodes)
                switch (node.Attributes["key"].Value)
                    case "Domain":
                        nc.Domain = node.Attributes["value"].Value;
                    case "UserName":
                        nc.UserName = node.Attributes["value"].Value;
                    case "Password":
                        nc.Password = node.Attributes["value"].Value;
                    case "AuthenticationType":
                        token.AuthenticationType = Convert.ToInt32(node.Attributes["value"].Value);
                    case "OrgName":
                        token.OrganizationName = node.Attributes["value"].Value;
                    case "CrmUrl":
                        crmService.Url = node.Attributes["value"].Value;

The purpose of the AppDomain.CurrentDomain.BaseDirectory property is to locate the actual path of the \bin\Debug folder

Create a free website or blog at WordPress.com.