Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

path to file in class library

I have a class libary project (NotificationTemplate) which return content of file:

public static class Template
{
    public static string NotificatioEmail
    {
        get { return File.ReadAllText("Templates\\NotificatioEmail.cshtml"); }
    }
}

In this project is located folder Templates with NotificatioEmail.cshtml file.

Also, I have two application: console app and ASP.NET MVC app. The file returned fine in the console app, but in the MVC I get error file not found. How to write universally?
I tryed this:

Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Templates",
             "NotificatioEmail.cshtml")

but BaseDirectory don't include bin folder.

like image 849
user348173 Avatar asked Mar 14 '13 05:03

user348173


Video Answer


1 Answers

As you said BaseDirectory works for console apps. For MVC applications, use RelativeSearchPath instead.

So you can use this code for both platforms

var appDomain = System.AppDomain.CurrentDomain;
var basePath = appDomain.RelativeSearchPath ?? appDomain.BaseDirectory;
Path.Combine(basePath, "Templates", "NotificatioEmail.cshtml");
like image 89
p.s.w.g Avatar answered Nov 10 '22 05:11

p.s.w.g