如何找到asp.net物理应用程序路径 [英] How to find the asp.net physical application path

查看:176
本文介绍了如何找到asp.net物理应用程序路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我在asp.net应用程序内使用控制台应用程序通过
发送html模板 mail.但是它将显示错误消息找不到路径的一部分.我正在使用httpcontext
查找路径还有其他方法可以不使用httpcontext来查找路径.我的代码是

方法1:
//const string TemplatePath =〜/HTMLPage.htm";//错误可能找到路径
////var reader = new StreamReader(TemplatePath).ReadToEnd();

方法2;
/////StreamReader reader = new StreamReader(System.Web.HttpContext.Current.Server.MapPath(〜/HTMLPage.htm"));错误


我正在使用conosole应用程序作为asp.net应用程序内的一个单独项目.如果有人有

解决方案

如果您将文件存储在bin文件夹中,则可以这样做

 字符串 fileName = " ;
字符串 filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,fileName); 


问题不明确.在ASP.NET代码隐藏代码中,您当然应该使用MapPath.对于您在服务器上执行的单独应用程序,此功能并不意味着任何确定的事情.您可以通过以下方式找到当前正在运行的应用程序所在的路径:

 字符串 exeDir = System.IO.Path.GetDirectoryName(
   System.Reflection.Assembly.GetEntryAssembly().Location; 



顺便说一句,还有许多其他方法可以找到此路径,但是其中一些方法不是通用的,对于某些承载应用程序的方法,它们无法正常工作,等等.我展示的方法是普遍正确的.

—SA


下面的代码行将起作用.

System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath

它将为您提供托管的物理路径


hi i am using console application inside asp.net application to send a html template through
mail.but it will display the error message could not find part of the path.i am using httpcontext
to find the path is there any other way to find the path without using httpcontext.my code is

method 1:
// const string TemplatePath = "~/HTMLPage.htm";//error could find path
// //var reader = new StreamReader(TemplatePath).ReadToEnd();

method 2;
//// StreamReader reader = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/HTMLPage.htm"));error


i am using conosole application as a seperate project inside asp.net application.if anyone have
any idea how to specify the path help me.

解决方案

if you file in bin folder then you can do like this

string fileName = "HTMLPage.htm";            
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);


The question is not clear. In an ASP.NET code-behind code, you should of course use MapPath. For your separate application you execute on the server, this function does not mean anything certain. You can find the path where the currently running application is located this way:

string exeDir = System.IO.Path.GetDirectoryName(
   System.Reflection.Assembly.GetEntryAssembly().Location;



By the way, there are many other ways to find this path, but some of them are not universal, they work incorrectly for some ways of hosting the application, etc. The method I show is universally correct.

—SA


The following line of code will work.

System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath

It will give you hosting physical path


这篇关于如何找到asp.net物理应用程序路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆