C# Read <system.net><mailSettings> in web.config from external dll
My web app calls an external dll. Within the dll I want to access the specifiedPickupDirectory pickupDirectoryLocation value within the system.net/mailSettings/smtp section. How can I grab it from within the dll co开发者_StackOverflowde?
Something like
System.Configuration.ConfigurationSettings.GetConfig("configuration/system.net/mailSettings/smtp/specifiedPickupDirectory/pickupDirectoryLocation")
but that doesn't work
You can use:
public string GetPickupDirectory()
{
var config = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;
return (config != null) ? config.SpecifiedPickupDirectory : null;
}
I guess you could simply use the PickupDirectoryLocation property.
// if .NET 4.0 don't forget that SmtpClient is IDisposable
SmtpClient client = new SmtpClient();
string pickupLocation = client.PickupDirectoryLocation;
This way you are not using magic strings in your code and it makes one less thing to worry about if in future versions of the framework this attribute changes name or location in the configuration file.
use this:
using System.Configuration;
using System.Web.Configuration;
using System.Net.Configuration;
then:
Configuration config = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup settings = (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");
then you'll have access to
//settings.Smtp.SpecifiedPickupDirectory;
Of course this should also be found in the System.Net.Mail.SmtpClient.PickupDirectoryLocation property
精彩评论