Is it possible to use a config file with macros?

Is it possible to use a config file with macros?

paul.t.macknight
Advocate Advocate
635 Views
0 Replies
Message 1 of 1

Is it possible to use a config file with macros?

paul.t.macknight
Advocate
Advocate

I followed this guide basically verbatim, and then straight copied and pasted (with some slight modifications to namespace lines and references) this guide for custom config files. I have triple checked both attempts as well as looked at other sources for the configuration namespace, but no matter what I do it returns my custom section as null. I do not currently have the means to break this out into an addin yet, and was hoping to work through the kinks on the config read/write part before I do. Any help would be greatly appreciated. Below is my code for reference, in case my eyes have grown accustomed to a blaring issue I missed:

 

This is the custom config module, within the macro module but in a separate cs file:

 

 

using System;
using System.Configuration;

namespace UsefulTools
{
	public class ConfigElement : ConfigurationElement
	{
		[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
		public string Name
		{
			get { return (string)base["name"]; }
			set { base["name"] = value; }
		}
		
		[ConfigurationProperty("isChecked", IsRequired = true)]
		public bool Include_Value
		{
			get { return (Boolean)base["isChecked"]; }
			set { base["isChecked"] = value; }
		}
	}
	
	public class ConfigCollection : ConfigurationElementCollection
	{
		public ConfigElement this[int index]
		{
			get { return (ConfigElement)BaseGet(index); }
			set
			{
				if (BaseGet(index) != null)
					BaseRemoveAt(index);
				
				BaseAdd(index, value);
			}
			
		}
		
		public new ConfigElement this[string key]
		{
			get { return (ConfigElement)BaseGet(key); }
			set
			{
				if (BaseGet(key) != null)
					BaseRemoveAt(BaseIndexOf(BaseGet(key)));
				
				BaseAdd(value);
			}
		}
		
		protected override ConfigurationElement CreateNewElement()
		{
			return new ConfigElement();
		}
		
		protected override object GetElementKey(ConfigurationElement element)
		{
			return ((ConfigElement)element).Name;
		}
	}
	
	public class ConfigSection : ConfigurationSection
	{
		[ConfigurationProperty("settings")]
		
		[ConfigurationCollection(typeof(ConfigCollection))]
		
		public ConfigCollection Configs
		{
			get { return (ConfigCollection)this["settings"]; }
		}
		
		public static ConfigSection GetConfiguration(string section)
		{
			return ConfigurationManager.GetSection(section) as ConfigSection;
		}
		
		private ConfigSection () : base () {
			// Allow this section to be stored in user.app. By default this is forbidden.
			this.SectionInformation.AllowExeDefinition =
				ConfigurationAllowExeDefinition.MachineToLocalUser;
		}
	}
}

 

 

This is, with very minor changes to suit my needs, the App.config file from the linked guide:

 

 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="Excel"
                 type="<macro module/namespace>.ConfigSection, <macro module/namespace>"/>
    </configSections>
    <Excel>
        <settings
            <add name="Header"
                 include_value="true"/>
            <add name="Column Headers"
                 include_value="true"/>
        </settings>
    </Excel>
</configuration>

 

 

And here is where I'm calling it:

 

 

var testConfig = (ConfigSection)ConfigurationManager.GetSection("Excel");
String name = null;
Boolean include_value = false;

/* 
 * MainContent displays as blank when the .Show() method is invoked.
 * I have tested other methods and variables here, and they all return blank or as null values.
 */
test.Title = "Config Test";
test.MainContent = string.Format("{0}", testConfig);
test.CommonButtons = TaskDialogCommonButtons.Close;
test.DefaultButton = TaskDialogResult.Close;
test.Show();

/* 
 * Error thrown at line below (foreach loop)
 * System.NullReferenceException: Object reference not set to an instance of an object.
 */
foreach (FluorToolsConfigElement setting in testConfig.FluorToolsConfigs)
{
	name = setting.Name;
	include_value = setting.Include_Value;
}

 

 

 

As they are both in the same namespace/macro module (UsefulTools), I do not have a line at the top of the main ThisApplication.cs to reference the classes. I have tested with a separate namespace, but no luck. I have changed the properties of the App.config file to be build action: content and have tried with Copy to output directory set to each value.

0 Likes
636 Views
0 Replies
Replies (0)