如何指定web.config文件的路径 [英] How to specify the path to the web.config file
本文介绍了如何指定web.config文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我想从Web配置文件中读取网络设置,并最终遇到如下所示的异常.我用这个pl编写了代码,告诉我如何指定web.config文件的路径.
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文件:
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>
例外:
``E:\ Abinav \ Leave_management_system_30_01_2012 \ LeaveManagementSystem_30_01_2012 \ LeaveManagementSystem_30_01_2012''不是有效的虚拟路径.
Exception:
''E:\Abinav\Leave_management_system_30_01_2012\LeaveManagementSystem_30_01_2012\LeaveManagementSystem_30_01_2012'' is not a valid virtual path.
推荐答案
您好,
试试下面的代码,
Hi,
Try the below code,
Configuration configurationFile = WebConfigurationManager
.OpenWebConfiguration("~/web.config");
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;
}
或也可以使用,
or can also use,
Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
希望这会有所帮助.
Hope this helps.
这篇关于如何指定web.config文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文