Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Server.MapPath inside class library project

I have a web application thet has a number of class library projects. Some example code below.

public static class LenderBL
{
    static string LenderXml { get { return "MyPathHere"; } }

    public static LenderColl GetLenders()
    {
        var serializer = new XmlSerializer(typeof(LenderColl));

        using (XmlReader reader = XmlReader.Create(LenderXml))
        {
            return (LenderColl)serializer.Deserialize(reader);
        }
    }
}

I would normally use Server.MapPath to get the path for the property LenderXml, but when I use it in a class library is returns the path of the parent solution, not that of the class library project.

Is there a way of getting the path for the class libary project itself?

Thanks in advance.

like image 890
dotnetnoob Avatar asked Apr 15 '13 07:04

dotnetnoob


People also ask

What is the use of server MapPath in asp net?

Because the MapPath method maps a path regardless of whether the specified directories currently exist, you can use the MapPath method to map a path to a physical directory structure, and then pass that path to a component that creates the specified directory or file on the server.

How do I run a class library in Visual Studio?

Right-click on the solution in Solution Explorer and select Add > New Project. On the Add a new project page, enter library in the search box. Choose C# or Visual Basic from the Language list, and then choose All platforms from the Platform list. Choose the Class Library template, and then choose Next.


1 Answers

As I can understand you need the current assembly location

static public string CurrentAssemblyDirectory()
{
    string codeBase = Assembly.GetExecutingAssembly().CodeBase;
    UriBuilder uri = new UriBuilder(codeBase);
    string path = Uri.UnescapeDataString(uri.Path);
    return Path.GetDirectoryName(path);
}
like image 113
Menelaos Vergis Avatar answered Oct 01 '22 03:10

Menelaos Vergis