NetBeauty2: Make your. NET project output directory more refreshing

NetBeauty2: Make your. NET project output directory more refreshing

In. NET project development, as the complexity of the project increases, the number of dll files you rely on will gradually increase. This often leads to confusing output directories and inconvenient to manage and deploy.

最后更新 3/12/2024 5:14 AM
沙漠尽头的狼
预计阅读 8 分钟
分类
.NET
标签
.NET C# output directory NetBeauty2

In. NET project development, as the complexity of the project increases, the number of dll files you rely on will gradually increase. This often leads to confusing output directories and inconvenient to manage and deploy. The NetBeauty 2 open source project was born to solve this problem. It can help developers move. NET runtime and dependent dll files to specified directories when independently publishing. NET projects, thereby making the output directory cleaner and refreshing.

1. Introduction to NetBeauty 2

NetBeauty 2 is an open source. NET dependency library sorting tool. Its main role is to organize and optimize the output directory when a. NET project is released independently. With NetBeauty 2, developers can easily move. NET runtime and dependent dll files to specified directories, making the project's output directory clearer and easier to manage.

项目仓库地址:https://github.com/nulastudio/NetBeauty2

下图为优化后输出目录(.NET运行时及引用依赖库移到libraries目录,目录名可配置):

下图为极限优化后输出目录(查看 --hiddens 选项使用)

再来对比下未使用前输出目录(震撼吧!.NET运行时及相关依赖库全放在了根输出目录,.NET Framework可以配置privatePath,.NET Core可没那么方便):

2. support for

NetBeauty 2 NetCoreBeauty
support frames .NET Framework .NET Core 3.0+ .NET Core 2.0+
Support deployment model Framework-dependent deployment (FDD) Self-contained deployment (SCD) Framework-dependent executables (FDE) Self-contained deployment (SCD)
supported operating systems All win-x64 win-x86 win-arm64(.NET 6+) linux-x64 linux-arm linux-arm64 osx-x64 osx-arm64(.NET 6+)
Need Patched HostFXR No Yes(if use patch) Yes
Minimum Structure ~20 Files ~8 Files(if use patch) ~8 Files
How It Works STARTUP_HOOKS AssemblyLoadContext.Resolving AssemblyLoadContext.ResolvingUnmanagedDll + patched libhostfxr(if use patch) additionalProbingPaths(if use patch) patched libhostfxr additionalProbingPaths
Shared Runtime Yes Possible If Using patched libhostfxr Alone

3. How to use it?

3.1. preparations

Add NuGet packages to your. NET Core project (the main project that needs to be released):

dotnet add package nulastudio.NetBeauty

打开工程文件编辑(.csproj):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <PropertyGroup>
    <BeautySharedRuntimeMode>False</BeautySharedRuntimeMode>
    <!-- beauty into sub-directory, default is libs, quote with "" if contains space  -->
    <BeautyLibsDir Condition="$(BeautySharedRuntimeMode) == 'True'">../libraries</BeautyLibsDir>
    <BeautyLibsDir Condition="$(BeautySharedRuntimeMode) != 'True'">./libraries</BeautyLibsDir>
    <!-- dlls that you don't want to be moved or can not be moved -->
    <!-- <BeautyExcludes>dll1.dll;lib*;...</BeautyExcludes> -->
    <!-- dlls that end users never needed, so hide them -->
    <!-- <BeautyHiddens>hostfxr;hostpolicy;*.deps.json;*.runtimeconfig*.json</BeautyHiddens> -->
    <!-- set to True if you want to disable -->
    <DisableBeauty>False</DisableBeauty>
    <!-- set to False if you want to beauty on build -->
    <BeautyOnPublishOnly>False</BeautyOnPublishOnly>
    <!-- DO NOT TOUCH THIS OPTION -->
    <BeautyNoRuntimeInfo>False</BeautyNoRuntimeInfo>
    <!-- set to True if you want to allow 3rd debuggers(like dnSpy) debugs the app -->
    <BeautyEnableDebugging>False</BeautyEnableDebugging>
    <!-- the patch can reduce the file count -->
    <!-- set to False if you want to disable -->
    <!-- SCD Mode Feature Only -->
    <BeautyUsePatch>True</BeautyUsePatch>
    <!-- App Entry Dll = BeautyDir + BeautyAppHostDir + BeautyAppHostEntry -->
    <!-- see https://github.com/nulastudio/NetBeauty2#customize-apphost for more details -->
    <!-- relative path based on AppHostDir -->
    <!-- .NET Core Non Single-File Only -->
    <!-- <BeautyAppHostEntry>bin/MyApp.dll</BeautyAppHostEntry> -->
    <!-- relative path based on BeautyDir -->
    <!-- .NET Core Non Single-File Only -->
    <!-- <BeautyAppHostDir>..</BeautyAppHostDir> -->
    <!-- <BeautyAfterTasks></BeautyAfterTasks> -->
    <!-- valid values: Error|Detail|Info -->
    <BeautyLogLevel>Info</BeautyLogLevel>
    <!-- set to a repo mirror if you have troble in connecting github -->
    <!-- <BeautyGitCDN>https://gitee.com/liesauer/HostFXRPatcher</BeautyGitCDN> -->
    <!-- <BeautyGitTree>master</BeautyGitTree> -->
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="nulastudio.NetBeauty" Version="2.1.4.4" />
  </ItemGroup>

</Project>

运行 dotnet build 或者 dotnet publish, 优化输出操作会自动完成。

3.2. If the app has been released?

If your application has been released, you can use it like this (the webmaster has not tried it, this can be used as a post-release compensation):

Usage:
nbeauty2 [--loglevel=(Error|Detail|Info)] [--srmode] [--enabledebug] [--usepatch] [--hiddens=hiddenFiles] [--noruntimeinfo] [--roll-forward=<rollForward>] [--apphostentry=<appHostEntry>] [--apphostdir=<appHostDir>] <beautyDir> [<libsDir> [<excludes>]]

For example:

ncbeauty2 --usepatch --loglevel Detail --hiddens "hostfxr;hostpolicy;*.deps.json;*.runtimeconfig*.json" /path/to/publishDir libraries "dll1.dll;lib*;..."

3.3. Configure as a. NET Core global tool

dotnet tool install --global nulastudio.nbeauty

It will be automatically applied when the program is released after installation.

4. Various project usage examples

克隆仓库(https://github.com/nulastudio/NetBeauty2),里面有各种模版项目使用示例:

Test project name description
WPFTest WPF project (similar to Winform), default. NET 5
WebAppTest RazorPages project, default. NET 6
NetFxTest NET Framework WPF project (. NET 4.x, Winform similar)
ChromelyTest Quotes from Chrovely's. NET project
AvaloniaTest Avalonia UI project, default. NET 5

Little Knowledge 1

The Chromely NuGet package is a library for creating cross-platform desktop applications that provides a Chromium-based browser control. With Chromely, developers can use web technologies such as HTML, CSS, and JavaScript to build user interfaces for desktop applications while retaining access to local system resources.

The Chromely NuGet package provides a complete set of APIs and tools that allow developers to easily convert web applications to desktop applications without extensive code rewriting or modifications. It also supports various plug-ins and extensions so that developers can add additional features or customize existing features as needed.

In addition, Chromely also supports multiple programming languages and frameworks, such as C#,. NET Core, ASP.NET Core, etc., which allows developers to choose the technology stack they are most familiar with to build applications.

Little Knowledge 2

Avalonia UI is a cross-platform. NET UI framework that allows developers to use XAML and C#languages to create applications that can run on multiple platforms, including Windows, Linux, macOS, iOS, Android, and WebAssembly. Avalonia UI is designed to help developers build beautiful, modern graphical user interfaces (GUIs). It is compatible with all platforms that support. NET Standard 2.0, allowing developers to create native applications for multiple operating systems from a single code base. By using Avalonia UI, developers can take full advantage of the powerful features of the. NET ecosystem while achieving cross-platform compatibility, reducing development costs and improving development efficiency.

推荐开源控件库:irihitech/Semi.Avaloniairihitech/Ursa.Avalonia

5. summary

林德熙大佬分享过类似的包NuGet Gallery | dotnetCampus.PublishFolderCleaner 3.11.1,但该库说明只在Windows发布支持,大家可以对比使用,原文链接:PublishFolderCleaner 让你的 dotnet 应用发布文件夹更加整洁 - lindexi - 博客园 (cnblogs.com),再次给出本文介绍库NetBeauty2开源地址:

项目仓库地址:https://github.com/nulastudio/NetBeauty2

Reference:

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读
同分类 / 同标签 2/7/2026

Summary of experience in using AOT

From the very beginning of project creation, you should develop a good habit of conducting AOT release testing in a timely manner whenever new features are added or newer syntax is used.

继续阅读