Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get relative file path in a class library project that is being referenced by a web project

Tags:

c#

asp.net

I have an ASP.Net website that references a class library. In the class library I need to read a file into memory.

At the top level of my class library there is a folder called EmailTemplateHtml containing the file MailTemplate.html that I want to read in.

How can I do this?

like image 355
DreamToCode Avatar asked Aug 21 '14 06:08

DreamToCode


3 Answers

In Visual Studio, you can configure your library such that the file is copied into the build directory of any project that depends upon it. Then you can get the path to the build directory at runtime in order to read your file.

Step by step instructions, starting from a fresh solution:

  1. Create your application project and your class library project.
  2. Add a reference to the class library project from the application project via Properties->Add->Reference from the application's context menu in Solution Explorer:

    Screenshot showing the *Reference* option Screenshot showing *Reference Explorer*

  3. Create the file in your class library project that you need to read, then set its Copy to Output Directory property to either Copy always or Copy if newer via the Properties pane in Solution Explorer:

    Screenshot showing the *Copy to Output Directory* option

  4. From within either the class library project or your application (either will work with exactly the same code), reference your file relative to Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location). For example:

    using System.Reflection;
    using System.IO;
    
    namespace MyLibrary
    {
        public class MyClass
        {
            public static string ReadFoo()
            {
                var buildDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                var filePath = buildDir + @"\foo.txt";
                return File.ReadAllText(filePath);
            }
        }
    }
    

    (Note that back before .NET Core, you could use a file path relative to System.IO.Directory.GetCurrentDirectory() instead, but this doesn't work in a .NET Core application because the initial working directory for .NET Core apps is the source directory instead of the build directory, apparently because this was needed by ASP.NET Core.)

  5. Go ahead and call your library code from your application code, and everything will work fine. e.g.:

    using Microsoft.AspNetCore.Mvc;
    using MyLibrary;
    
    namespace AspCoreAppWithLib.Controllers
    {
        public class HelloWorldController : Controller
        {
            [HttpGet("/read-file")]
            public string ReadFileFromLibrary()
            {
                return MyClass.ReadFoo();
            }
        }
    }
    
like image 159
Mark Amery Avatar answered Sep 23 '22 07:09

Mark Amery


using System.IO;    
using System.Reflection;

public static string ExecutionDirectoryPathName()
    {
         var dirPath = Assembly.GetExecutingAssembly().Location;
         dirPath = Path.GetDirectoryName(dirPath);
         return Path.GetFullPath(Path.Combine(dirPath, "\EmailTemplateHtml\MailTemplate.html"));
    }
like image 26
Neeraj Dubey Avatar answered Sep 22 '22 07:09

Neeraj Dubey


If you want to find the path where the assembly is located; from within the assembly then use the following code:

 public static string ExecutionDirectoryPathName
 {
   get
     {
         var dirPath = Assembly.GetExecutingAssembly().Location;
         dirPath = Path.GetDirectoryName(dirPath);
         return dirPath + @"\";
     }
 }
like image 33
SamCoder Avatar answered Sep 19 '22 07:09

SamCoder