How to get relative path from absolute path

前端 未结 23 2057
既然无缘
既然无缘 2020-11-22 11:52

There\'s a part in my apps that displays the file path loaded by the user through OpenFileDialog. It\'s taking up too much space to display the whole path, but I don\'t want

23条回答
  •  醉梦人生
    2020-11-22 12:48

    In ASP.NET Core 2, if you want the relative path to bin\Debug\netcoreapp2.2 you can use the following combination:

    using Microsoft.AspNetCore.Hosting;
    using Microsoft.Extensions.Configuration;
    public class RenderingService : IRenderingService
    {
    
        private readonly IHostingEnvironment _hostingEnvironment;
        public RenderingService(IHostingEnvironment hostingEnvironment)
        {
        _hostingEnvironment = hostingEnvironment;
        }
    
        public string RelativeAssemblyDirectory()
        {
            var contentRootPath = _hostingEnvironment.ContentRootPath;
            string executingAssemblyDirectoryAbsolutePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            string executingAssemblyDirectoryRelativePath = System.IO.Path.GetRelativePath(contentRootPath, executingAssemblyDirectoryAbsolutePath);
            return executingAssemblyDirectoryRelativePath;
        }
    }
    

提交回复
热议问题