views:

88

answers:

3

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 code?

Something like

System.Configuration.ConfigurationSettings.GetConfig("configuration/system.net/mailSettings/smtp/specifiedPickupDirectory/pickupDirectoryLocation")

but that doesn't work

+1  A: 

You can use:

public string GetPickupDirectory()
{
    var config = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as SmtpSection;

    return (config != null) ? config.SpecifiedPickupDirectory : null;
}
Matthew Abbott
Great - thanks very much. I tweaked to deliver PickupDirectoryLocation.public string GetPickupDir() { var config =System.Configuration.ConfigurationManager.GetSection("system.net/mailSettings/smtp") as System.Net.Configuration.SmtpSection; return (config != null) ? config.SpecifiedPickupDirectory.PickupDirectoryLocation : null; }
DEH
+1  A: 

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.

Darin Dimitrov
Good point, the SmtpClient automatically uses the configuration settings.
Matthew Abbott
A: 

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

matt-dot-net