How to access the Web.Config and ProjectProperties at Design Time
You can use this in a design time context, where you have an ISite or annother ServiceProvider. I use a version with more properties in my designers.
DesignTimeConfig config =
new DesignTimeConfig(this.Site);Then use config.WebConfig to manipulate the XmlDocument...
using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using System.Resources;
using System.Xml;
using EnvDTE;
namespace UITemplates
{
public class DesignTimeConfig
{
private XmlDocument _webConfig;
private EnvDTE.ProjectItem _projectItem;
private IServiceProvider _serviceProvider;
public DesignTimeConfig(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public object GetService(Type type)
{
return _serviceProvider.GetService(type);
}
public XmlDocument WebConfig
{
get
{
string projectPath;
string filename;
if(_webConfig!=null)
{
return _webConfig;
}
XmlDocument configXml = new XmlDocument();
projectPath = ProjectProperties.Item("ActiveFileSharePath").Value.ToString();
filename = projectPath+ @"\web.config";
configXml.Load(filename );
_webConfig = configXml;
return configXml;
}
}
internal EnvDTE.Properties ProjectProperties
{
get
{
return ProjectItem.ContainingProject.Properties;
}
}
internal EnvDTE.ProjectItem ProjectItem
{
get
{
if(_projectItem==null)
{
_projectItem =(EnvDTE.ProjectItem)GetService(typeof(EnvDTE.ProjectItem));;
}
return _projectItem;
}
}
/// <summary>
/// Gets the value from an <appSettings><add /> element
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string AppSettings(string key)
{
XmlNode designLangNode = WebConfig.SelectSingleNode("//appSettings/add[@key='" + key + "']");
if(designLangNode==null)
{
return String.Empty;
}
XmlAttribute currentValue = designLangNode.Attributes["value"];
if(currentValue==null)
{
return String.Empty;
}
return currentValue.Value;
}
public string GetProjectProperty(string name)
{
return ProjectProperties.Item(name).Value.ToString();
}
}
}
There is a posting here about it here, but as I commented, I prefer Site.GetService :
http://blogs.msdn.com/mszcool/archive/2004/06/30/169793.aspx
To get the project properties, you can call GetProjectProperty with the names from here : http://msdn.microsoft.com/library/en-us/vbcon/html/vbconthemacroprojectobjectmodel.asp?frame=true