将C#内嵌文件释放到指定位置

using System;
using System.IO;
using System.Reflection;
using ConsoleTables;

namespace EmbeddedFileExtractor
{
    public static class ResourceExtractor
    {
       
        public static bool ExtractResourceToFile(string resourceName, string targetFilePath)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            string[] resourceNames = assembly.GetManifestResourceNames();

            if (!Array.Exists(resourceNames, r => r == resourceName))
            {
                Console.WriteLine("指定的资源未找到.");
                return false;
            }

            Stream stream = assembly.GetManifestResourceStream(resourceName);

            if (stream == null)
            {
                Console.WriteLine("未能获取资源流.");
                return false;
            }

            try
            {
                using (FileStream fileStream = new FileStream(targetFilePath, FileMode.Create))
                {
                    stream.CopyTo(fileStream);
                }
                Console.WriteLine("已成功释放: {0}", resourceName);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("文件释放失败: {0}", ex.Message);
                return false;
            }
        }
    }

    class Program
    {
        static void 释放(KeyValuePair<string, List<string>> kvp)
        {
            string key = kvp.Key;

            string name = "自释放." + key;
            string targetPath = Path.Combine(Directory.GetCurrentDirectory(), "自释放" , key);
            string directoryPath = Path.GetDirectoryName(targetPath);
            if (directoryPath != null && !Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
                Console.WriteLine("目录不存在,已创建");
            }
            ResourceExtractor.ExtractResourceToFile(name,targetPath);
        }
        static void 废话(){
            Console.WriteLine("按任意键退出");
            Console.ReadKey();
        }
        static void Main(string[] args)
        {
            Dictionary<string, List<string>> files = new Dictionary<string, List<string>>();

            files.TryAdd("六构图.pdf", new List<string> { "几个化学结构式" });
            if (args.Length >0)
            {
                foreach (var kvp in files)
                {
                    string name = kvp.Key;
                    释放(kvp);
                }
                Console.WriteLine("释放完毕");
                废话();
            }
            
            ConsoleTable table = new ConsoleTable("文件名", "描述");
            Console.WriteLine("编译日期:   ");
            foreach (var kvp in files)
            {
                string name = kvp.Key;
                List<string> info = kvp.Value;
                table.AddRow(name, info[0]);
            }
            table.Write();
            Console.WriteLine();
            Console.WriteLine("是否进行释放操作(Y/N)");
            ConsoleKeyInfo Is_next = Console.ReadKey();
            Console.WriteLine();
            if (Is_next.Key == ConsoleKey.Y || Is_next.Key == ConsoleKey.Y /* 大写Y */)
            {
                foreach (var kvp in files)
                {
                    string name = kvp.Key;
                    释放(kvp);
                }
                Console.WriteLine("释放完毕");
                废话();
            }
            else if (Is_next.Key == ConsoleKey.N || Is_next.Key == ConsoleKey.N /* 大写Y */)
            {
                Console.WriteLine("释放已取消");
                废话();
            }
            else
            {
                Console.WriteLine("无效的输入");
                废话();
            }
        }
    }
}

 

如题,这段代码会将内嵌在项目中的文件释放出来,并配有一个控制台列表来展示文件名和文件描述,但需要手动配置files字典来配置文件列表

注意要把文件属性改成“内嵌的资源”

释放内嵌的资源文件指定位置,你可以使用以下步骤: 1. 确保你的资源文件已经嵌入到了程序集中。可以在 Visual Studio 中的资源文件管理器中确认。 2. 使用 `Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)` 方法获取嵌入资源的流。其中 `resourceName` 是你的资源文件的完整名称,包括命名空间和文件名。 3. 创建一个新的文件流,指定你想要将资源文件释放到的位置。 4. 通过将嵌入资源的流复制到新的文件流中,将资源文件释放指定位置。你可以使用 `Stream.CopyTo()` 方法来实现这一点。 下面是一个示例代码,展示了如何释放内嵌的资源文件指定位置: ```csharp using System; using System.IO; using System.Reflection; public class ResourceManager { public static void ExtractResourceToFile(string resourceName, string targetPath) { using (Stream resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) { if (resourceStream == null) { throw new ArgumentException($"Resource '{resourceName}' not found."); } using (FileStream fileStream = File.Create(targetPath)) { resourceStream.CopyTo(fileStream); } } } } ``` 你可以调用 `ResourceManager.ExtractResourceToFile(resourceName, targetPath)` 方法,将资源文件释放指定位置。其中 `resourceName` 是嵌入资源的完整名称,`targetPath` 是要释放到的目标位置文件路径。 希望这能帮助到你!如果有任何问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值