Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

Posted
Updated 30-May-19 19:47pm

if you file in bin folder then you can do like this
C#
string fileName = "HTMLPage.htm";            
string filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
 
Share this answer
 
Comments
prabhakar78 8-May-14 8:17am    
hi my html template is inside the asp.net application and i try to call that html template
from the console application which is also present inside the same application.anyway
thanks for your suggestion
Vi(ky 8-May-14 8:34am    
when you run you console app in run from debug/ release folder inside bin folder. to get access your html file you have to put the file in debug/ or release folder & then you can use
string fileName = "HTMLPage.htm";
string folder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string filePath = Path.Combine(folder, fileName);
prabhakar78 8-May-14 8:58am    
ok thnaks
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:
C#
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
 
Share this answer
 
The following line of code will work.

Var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

It will give you hosting physical path. You can concatenate with sub folder further.
 
Share this answer
 
v3
Comments
CHill60 31-May-19 7:45am    
Actually that line of code won't. It won't even compile. I think you meant
string physicalPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
Member 12818842 5-Jun-19 6:07am    
Yes, it's same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900