当前位置: 首页 > news >正文

C# .Net 发布后,把dll全部放在一个文件夹中,让软件目录更整洁

PublishFolderCleaner – Github

测试环境:

.Net 8

Program.cs 代码

// https://github.com/dotnet-campus/dotnetcampus.DotNETBuildSDK/tree/master/PublishFolderCleanerusing System.Diagnostics;
using System.Text;// 名称, 不用写 .exe
var exeName = "AbpDemo";
// 目录
var publishFolder = "D:\\Work\\CSharpProject\\AbpDemo\\AbpDemo\\bin\\Debug\\net8.0";const string libFolderName = "lib";
var libFolder = Path.GetFullPath(Path.Combine(publishFolder, libFolderName));
var tempFolder = Path.GetFullPath(Path.Combine(publishFolder, @"..", Path.GetRandomFileName()));
Directory.Move(publishFolder, tempFolder);
Directory.CreateDirectory(publishFolder);
Directory.Move(tempFolder, libFolder);var appHostFilePath = Path.Combine(libFolder, exeName + ".exe");
var newAppHostFilePath = Path.Combine(publishFolder, exeName + ".exe");File.Move(appHostFilePath, newAppHostFilePath);Patch(newAppHostFilePath, Path.Combine("lib", exeName + ".dll"));/// <summary>
/// 这里有 1024 个 byte 空间用来决定加载路径
/// 详细请看 dotnet runtime\src\installer\corehost\corehost.cpp 的注释
/// </summary>
const int MaxPathBytes = 1024;int Patch(string appHostExe, string newPath)
{try{var origPath = Path.GetFileName(ChangeExecutableExtension(appHostExe));if (!File.Exists(appHostExe)){Console.WriteLine($"AppHost '{appHostExe}' does not exist");return 1;}if (origPath == string.Empty){Console.WriteLine("Original path is empty");return 1;}var origPathBytes = Encoding.UTF8.GetBytes(origPath + "\0");Debug.Assert(origPathBytes.Length > 0);var newPathBytes = Encoding.UTF8.GetBytes(newPath + "\0");if (origPathBytes.Length > MaxPathBytes){Console.WriteLine($"Original path is too long");return 1;}if (newPathBytes.Length > MaxPathBytes){Console.WriteLine($"New path is too long");return 1;}var appHostExeBytes = File.ReadAllBytes(appHostExe);int offset = GetOffset(appHostExeBytes, origPathBytes);if (offset < 0){Console.WriteLine($"Could not find original path '{origPath}'");return 1;}if (offset + newPathBytes.Length > appHostExeBytes.Length){Console.WriteLine($"New path is too long: {newPath}");return 1;}for (int i = 0; i < newPathBytes.Length; i++)appHostExeBytes[offset + i] = newPathBytes[i];File.WriteAllBytes(appHostExe, appHostExeBytes);return 0;}catch (Exception ex){Console.WriteLine(ex.ToString());return 1;}
}string ChangeExecutableExtension(string apphostExe) =>// Windows apphosts have an .exe extension. Don't call Path.ChangeExtension() unless it's guaranteed// to have an .exe extension, eg. 'some.file' => 'some.file.dll', not 'some.dll'apphostExe.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) ? Path.ChangeExtension(apphostExe, ".dll") : apphostExe + ".dll";int GetOffset(byte[] bytes, byte[] pattern)
{int si = 0;var b = pattern[0];while (si < bytes.Length){si = Array.IndexOf(bytes, b, si);if (si < 0)break;if (Match(bytes, si, pattern))return si;si++;}return -1;
}bool Match(byte[] bytes, int index, byte[] pattern)
{if (index + pattern.Length > bytes.Length)return false;for (int i = 0; i < pattern.Length; i++){if (bytes[index + i] != pattern[i])return false;}return true;
}

效果真不错

在这里插入图片描述

http://www.lryc.cn/news/306339.html

相关文章:

  • [更新]ARCGIS之土地耕地占补平衡、进出平衡系统报备坐标txt格式批量导出工具(定制开发版)
  • todolist
  • 【Java程序设计】【C00307】基于Springboot的基Hadoop的物品租赁管理系统(有论文)
  • GIT中对子仓库的使用方法介绍
  • ClickHouse 指南(三)最佳实践 -- 跳数索引
  • Mybatis总结--传参二
  • 2024年数字化转型风口趋势大赏
  • 某款服务器插上4张TDP功耗75瓦PCIE卡无法开机的调试过程
  • 数据结构与算法——排序算法
  • 阿里巴巴alibaba API商品详情接口系列(商品属性,价格,主图)阿里巴巴alibaba根据ID取商品详情 API 返回值说明
  • lcd画圆
  • React组件详解
  • C++面试:内存溢出、内存泄漏的原因与解决
  • 【Java程序员面试专栏 算法思维】二 高频面试算法题:二分查找
  • kaldi 详细安装教程、PyTorch-Kaldi、TIMIT下载、Librispeech下载
  • EtherCAT 转 ModbusTCP 网关
  • iMazing2024Windows和Mac的iOS设备管理软件(可以替代iTunes进行数据备份和管理)
  • carpower
  • 数据结构2月25日
  • 改进 RAG:自查询检索
  • 【Git企业实战开发】Git常用开发流操作总结
  • vue2+element医院安全(不良)事件报告管理系统源代码
  • leetcode初级算法(python)- 字符串
  • Python 鼠标模拟
  • Linux进程 ----- 信号处理
  • 【数位】【数论】【分类讨论】2999. 统计强大整数的数目
  • MongoDB聚合运算符:$atan2
  • 敏捷开发最佳实践:价值维度实践案例之ABTest中台化
  • 爬虫基本库的使用(requests库的详细解析)
  • QT实现串口通信