版本号
.net 8
代码
using System.Reflection;namespace Demo;internal class Program
{static void Main(string[] args){// 获取当前 执行exe 的目录 / 当前命令行所在的目录 var currentDir = Directory.GetCurrentDirectory();Console.WriteLine(currentDir);ExtractAllFile(currentDir);Console.WriteLine("文件释放结束");}// 把嵌入资源按照 原路径 输出到某个本地文件夹// 嵌入资源的名称可以用 dnSpy 查看..// string outDirPath = "E:\work"// Demo.public.abc.png -> E:\work\public\abc.png// Demo.abc.png -> E:\work\abc.pngstatic void ExtractAllFile(string outDirPath){var assembly = Assembly.GetExecutingAssembly();// 获取所有嵌入的文件// 格式:命名空间.文件夹.文件名 // Demo.public.abc.pngvar names = assembly.GetManifestResourceNames();foreach (var name in names){// 读取嵌入的文件 streamvar streamData = assembly.GetManifestResourceStream(name);if (streamData is not null){var resourcePathArr = name.Split('.');// 文件夹完整地址var dirPathArr = resourcePathArr[1..^2];var dirPath = Path.Combine(outDirPath, Path.Combine(dirPathArr));// 文件名+扩展名 (abc.png)var fileNameArr = resourcePathArr[^2..];var fileName = string.Join(".", fileNameArr);if (!Directory.Exists(dirPath)){// 创建文件夹Directory.CreateDirectory(dirPath);}using var inStream = new BufferedStream(streamData);// 创建/覆盖 文件using var outStream = new FileStream(Path.Combine(dirPath, fileName), FileMode.Create, FileAccess.Write);byte[] buffer = new byte[1024];int length;while ((length = inStream.Read(buffer, 0, buffer.Length)) > 0){outStream.Write(buffer, 0, length);}outStream.Flush();}}}}