.net6+ 在单文件应用程序中获取程序集位置
一般来说,获取执行程序集的位置,您可以调用:
var executableDirectory = System.Reflection.Assembly.GetExecutingAssembly().Location;
如果发布为单个文件, 会提示如下警告
warning IL3000: 'System.Reflection.Assembly.Location' always returns an empty string for assemblies embedded in a single-file app. If the path to the app directory is needed, consider calling 'System.AppContext.BaseDirectory'.
需要改为 AppContextBaseDirectory获取当前目录
var executableDirectory = System.AppContext.BaseDirectory;
示例用法
例如,您可以使用此路径来设置当前目录:
var executableDirectory = AppContext.BaseDirectory;
Directory.SetCurrentDirectory(executableDirectory ?? throw new Exception("Could not find out executable directory"));
或者指示配置文件提供者而不改变当前目录:
var builder = WebApplication.CreateBuilder(args);
builder.Configuration.SetBasePath(AppContext.BaseDirectory);
builder.Configuration.AddJsonFile("CustomConfig.json");
参考:
https://www.softwaredeveloper.blog/executing-assembly-location-in-a-single-file-app
关联项目
FreeSql QQ群:4336577
BA & Blazor QQ群:795206915
Maui Blazor 中文社区 QQ群:645660665
知识共享许可协议
本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。欢迎转载、使用、重新发布,但务必保留文章署名AlexChow(包含链接: https://github.com/densen2014 ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。如有任何疑问,请与我联系 。
转载声明
本文来自博客园,作者:周创琳 AlexChow,转载请注明原文链接:https://www.cnblogs.com/densen2014/p/18279144
AlexChow
今日头条 | 博客园 | 知乎 | Gitee | GitHub

 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号