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

Asked By abinav shankar on 30-Jan-12 07:23 AM
Hi
 
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:

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


Exception:

'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.


public
static Configuration OpenConfigFile(string configPath)

{
   
var configFile = new FileInfo(configPath);
   
var vdm = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
   
var wcfm = new WebConfigurationFileMap();
    wcfm
.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.



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

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

for more detail

http://msdn.microsoft.com/en-us/library/ie/610xe886.aspx
Regards
D
dipa ahuja replied to abinav shankar on 30-Jan-12 07:49 AM
Step 1 : Add this in Web.config
    
<system.net>
<mailSettings>
<smtp>
<network host="smtp.gmail.com"
  port="587"
  userName=""
  password=""
</smtp>
</mailSettings>
</system.net>
  
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.To.Add(toEmailAddress);
 
  mail.From = new MailAddress(GmailId);
  mail.Subject = subject;
  mail.Body = bodyMsg;
  mail.IsBodyHtml = true;
 
    SmtpClient smtp = new SmtpClient();
  smtp.EnableSsl = true;
  smtp.Send(mail);
}
 
 
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
hi,

Try for specifiying path

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

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
http://weblogs.asp.net/sahannet/archive/2008/02/13/asp-net-web-config-file.aspx
http://stackoverflow.com/questions/4616646/specifying-relative-file-location-in-web-config-for-use-by-standard-c-sharp-clas