DreamToCode
DreamToCode

Reputation: 291

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

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?

Upvotes: 23

Views: 39823

Answers (5)

Neeraj
Neeraj

Reputation: 4489

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"));
    }

Upvotes: 7

Virendra Deshmukh
Virendra Deshmukh

Reputation: 1

You can add a Static class in your class library with some static methods/properties to set. Set the values from Global.ascx.cs on start app method. Now you can get the values of class library.

Hope this makes clear. Happy coding

Upvotes: -4

Mark Amery
Mark Amery

Reputation: 154735

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();
            }
        }
    }
    

Upvotes: 50

Lee
Lee

Reputation: 584

I am not sure what you mean by a folder in the class library but you can use the current working directory if you wish to build a path as follows:

System.IO.Directory.GetCurrentDirectory()

You can then use the Path.Combine() method to build file paths.

Upvotes: 0

SamCoder
SamCoder

Reputation: 41

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 + @"\";
     }
 }

Upvotes: 4

Related Questions