Get the relative path of a file in a class library project referenced by a web project

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

At the top level of my class library, there is a named folder EmailTemplateHtml

that contains the file MailTemplate.html

that I want to read.

How can i do this?

+10


source to share


5 answers


In Visual Studio, you can configure your library to copy the file to the build directory of any project that depends on it. Then you can get the path to the build directory at runtime to read your file.

Step by step instructions, starting with fresh mortar:



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

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

  3. Create a file in the class library project that you want to read, then set its Copy to Output Directory property to Always Copy or Copy if newer through the Properties pane in Solution Explorer:

    Screenshot showing the * Copy to Output Directory * option

  4. Within a class library project or your application (or will work with exactly the same code), specify 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 prior to .NET Core, you could have used the file path relative instead System.IO.Directory.GetCurrentDirectory()

    , but this does not work in a .NET Core app as the starting working directory for .NET Core apps is the source directory, not the build directory , apparently because it was necessary for ASP.NET Core.)

  5. Go ahead and name your library code from your application code and everything should work fine. eg:

    using Microsoft.AspNetCore.Mvc;
    using MyLibrary;
    
    namespace AspCoreAppWithLib.Controllers
    {
        public class HelloWorldController : Controller
        {
            [HttpGet("/read-file")]
            public string ReadFileFromLibrary()
            {
                return MyClass.ReadFoo();
            }
        }
    }
    
          

+21


source


public static string ExecutionDirectoryPathName
{
     var dirPath = Assembly.GetExecutingAssembly().Location;
     dirPath = Path.GetDirectoryName(dirPath);
     return Path.GetFullPath(Path.Combine(dirPath, "\EmailTemplateHtml\MailTemplate.html"));

}

      



+3


source


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

 public static string ExecutionDirectoryPathName
 {
   get
     {
         var dirPath = Assembly.GetExecutingAssembly().Location;
         dirPath = Path.GetDirectoryName(dirPath);
         return dirPath + @"\";
     }
 }

      

+2


source


I'm not sure what you mean by folder in the class library, but you can use the current working directory if you want to build the path like this:

System.IO.Directory.GetCurrentDirectory()

      

Then you can use the method Path.Combine()

to create file paths.

0


source


You can add a static class to your class library with some static methods / properties to be set. Set the values ​​from Global.ascx.cs in the application startup method. Now you can get the class library values.

Hope this is clear. Happy coding

0


source







All Articles