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

前端 未结 5 600
南方客
南方客 2020-12-08 16:48

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 fo

相关标签:
5条回答
  • 2020-12-08 17:22
    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"));
        }
    
    0 讨论(0)
  • 2020-12-08 17:27

    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

    0 讨论(0)
  • 2020-12-08 17:36

    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 + @"\";
         }
     }
    
    0 讨论(0)
  • 2020-12-08 17:43

    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:

    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:

    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();
              }
          }
      }
      
    0 讨论(0)
  • 2020-12-08 17:48

    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.

    0 讨论(0)
提交回复
热议问题