简体   繁体   English

如何配置独立的单文件程序?

[英]How to configure self-contained single-file program?

I am experimenting with .Net Core 3 trimmed single-file publishing creating a simple console application and using this to publish: 我正在尝试使用.Net Core 3修剪的单文件发布来创建一个简单的控制台应用程序,并使用它来发布:

dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true /p:PublishTrimmed=true

Configuration is done with the standard appsettings.json and ConfigurationBuilder from Microsoft.Extensions.Configuration.Json . 使用Microsoft.Extensions.Configuration.Json的标准appsettings.jsonConfigurationBuilder完成ConfigurationBuilder

The problem is the config file is packaged with the whole app and so is unavailable - it can be accessed in the temp dir created when running the program but that is not very useful for a regular user. 问题是配置文件与整个应用程序打包在一起,因此不可用-可以在运行程序时在创建的temp目录中访问它,但这对普通用户而言不是很有用。

I found out that you can exclude files from the packaging process by editing the csproj file or with a custom publishing profile 我发现您可以通过编辑csproj文件或使用自定义发布配置文件将文件从打包过程中排除

<ItemGroup>
    <None Update="appsettings.json">
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
      <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
    </None>
</ItemGroup>

This does output the appsettings file in the publish dir however it is not used when running the application - an empty config file is created and used instead. 这确实在发布目录中输出appsettings文件,但是在运行应用程序时不使用它-创建并使用一个空的配置文件。 Is there any way to do this "properly" since documentation is kinda lacking on the issue or should I just manually parse the file I expect to be in the starting directory (which itself was a hassle to find because Assembly.Get###Assembly were all returning the temp dir)? 有什么办法可以“适当地”执行此操作,因为在此问题上尚缺少文档,或者我应该手动解析我希望位于起始目录中的文件(由于Assembly.Get###Assembly ,查找目录本身很麻烦)都返回了临时目录)?

I've found that adding this to the ConfigurationBuilder() before the AddJsonFile() seems to do the trick: 我发现在AddJsonFile()之前将其添加到ConfigurationBuilder()似乎可以解决问题:

   .SetBasePath(Environment.CurrentDirectory)

ie: 即:

var configurationRoot = new ConfigurationBuilder()
        .SetBasePath(Environment.CurrentDirectory)
        .AddJsonFile("appSettings.json", optional: false)
        .Build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET 6.0 控制台应用程序作为独立的单文件可执行文件 - .NET 6.0 Console app as a self-contained single file executable 您能否将资源发送到 .NET 5 中独立的单文件应用程序中的进程? - Can you send resources to a process in a self-contained, single file application in .NET 5? 如何创建一个用C#编写的独立,自包含的安装程序? - How to create a standalone, self-contained setup program written in C#? .NET 4.8 没有为 Linux 创建一个独立的文件 - .NET 4.8 not creating a self-contained file for Linux 独立的普通纪念品 - Self-contained generic memento 是否可以将MsTest项目构建为独立的可执行文件? 怎么样? - Is it possible to build an MsTest project into a self-contained executable? How? 如何使用 self-contained=false 从源创建 MSIX pckage - How to create an MSIX pckage from source with self-contained=false .NET Core 自包含发布引用 - 如何删除未使用的? - .NET Core self-contained publish references - how to remove unused? 引用的项目是一个非独立的可执行文件。 自包含的可执行文件不能引用非自包含的可执行文件 - The referenced project is a non self-contained executable. A non self-contained executable cannot be referenced by a self-contained executable 获取自带EXE的路径? - Get self-contained EXE's path?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM