C# .NET - How to specify the path to the web.config file

Asked By abinav shankar on 30-Jan-12 07:23 AM
I want to read the network settings from the web config file and i ended up getting the exception as shown below I have atached the code with this pl tell me how to specify the path of the web.config file
public void sendEmail()
    string strConfigPath = Server.MapPath("~/LeaveManagementSystem_30_01_2012");
    Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(strConfigPath);
    MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
    if (mailSettings != null)
      int port = mailSettings.Smtp.Network.Port;
      string host = mailSettings.Smtp.Network.Host;
      string password = mailSettings.Smtp.Network.Password;
      string username = mailSettings.Smtp.Network.UserName;

Web.config file:

    <smtp from="abinav@kcubeconsulting.com">
    <network host="smtp.bizmail.yahoo.com" port="25" userName="Asd@yahoo.com" password="Password" defaultCredentials="true" />


'E:\Abinav\Leave_management_system_30_01_2012\LeaveManagementSystem_30_01_2012\LeaveManagementSystem_30_01_2012' is not a valid virtual path.

Jitendra Faye replied to abinav shankar on 30-Jan-12 07:28 AM

You will have to map the physicalPath to a virtualPath. Here is how you would do that.

static Configuration OpenConfigFile(string configPath)

var configFile = new FileInfo(configPath);
var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
var wcfm = new WebConfigurationFileMap();
.VirtualDirectories.Add("/", vdm);
return WebConfigurationManager.OpenMappedWebConfiguration(wcfm, "/");

let me know.
D Company replied to abinav shankar on 30-Jan-12 07:29 AM
Hello Friend ,

here is sample to read and specify config settings.

  <add key="customsetting1" value="Some text here"/>

System.Configuration.Configuration rootWebConfig1 =
   if (rootWebConfig1.AppSettings.Settings.Count > 0)
  System.Configuration.KeyValueConfigurationElement customSetting =
  if (customSetting != null)
   Console.WriteLine("customsetting1 application string = \"{0}\"",
   Console.WriteLine("No customsetting1 application string");

for more detail

dipa ahuja replied to abinav shankar on 30-Jan-12 07:49 AM
Step 1 : Add this in Web.config
<network host="smtp.gmail.com"
Step 2 : Mail Sending Code
using System.Net.Mail;
using System.Net;
protected void btnSent_Click(object sender, EventArgs e)
  string toEmailAddress = "xx@gmail.com";
  string GmailId = "abc@gmail.com";
  string bodyMsg = "helo its testing mail";
  string subject = "testing mail";
  MailMessage mail = new MailMessage();
  mail.From = new MailAddress(GmailId);
  mail.Subject = subject;
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
  smtp.EnableSsl = true;
abinav shankar replied to Jitendra Faye on 30-Jan-12 07:50 AM
Thanks a lot it works fine
Jitendra Faye replied to abinav shankar on 30-Jan-12 08:12 AM
You always welcome.
kalpana aparnathi replied to abinav shankar on 30-Jan-12 08:14 AM

Try for specifiying path

Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
smr replied to abinav shankar on 30-Jan-12 08:37 AM

You could use Path.Combine to combine AppDomain.CurrentDomain.BaseDirectory and your relative path.

This will give you a path relative to the ASP.NET root directory (~/) in an ASP.NET app, or a path relative to the directory containing the executable in a WinForms or Console application.

follow links for examples