C# 把dll打包到exe文件,真的可以 。文件批量转了ANSI编码

在 C# 中,将 DLL 文件打包到 EXE 文件中可以使用 ILRepack 工具。ILRepack 是一个开源的工具,可以合并多个 DLL 文件并将它们嵌入到一个 EXE 文件中,从而实现将 DLL 打包到 EXE 的功能。

以下是使用 ILRepack 工具打包 DLL 到 EXE 的步骤:

  1. 首先,在项目中使用 NuGet 包管理器将 ILRepack 安装到项目中。

  2. 在项目的输出文件夹中找到已编译的 EXE 文件和需要打包的 DLL 文件。

  3. 打开命令提示符(或 PowerShell),导航到已编译的 EXE 文件和 DLL 文件所在的文件夹。

  4. 在命令提示符中输入以下命令,并执行:

 
 

plaintextCopy code

ILRepack.exe /out:MergedApp.exe App.exe Lib1.dll Lib2.dll Lib3.dll ...

其中,MergedApp.exe 是要生成的合并后的 EXE 文件的名称,App.exe 是原始的 EXE 文件名,Lib1.dll, Lib2.dll, Lib3.dll 是需要打包到 EXE 中的 DLL 文件名列表。

  1. 执行命令后,ILRepack 将会合并指定的 EXE 文件和 DLL 文件,并将合并后的文件保存为 MergedApp.exe

完成以上步骤后,您将获得一个包含 DLL 文件的 EXE 文件。您可以运行该 EXE 文件,并使用其中的功能,无需再单独依赖 DLL 文件。请注意,此方法适用于.NET Framework 项目。对于 .NET Core 项目,可以使用 .NET Core 自带的 dotnet publish 命令进行类似的操作。

cd C:\Users\wuqimei\source\repos\FileConvertion\FileConvertion\bin\Release
ILRepack.exe /out:FileConvertionM.exe FileConvertion.exe Ude.NetStandard.dll

  

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ude;

namespace FileConvertion
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button_SelectFolder_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
             显示选择文件夹的对话框
            //FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
            //if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            //{
            //    string folderPath = folderBrowserDialog.SelectedPath;
            //    this.Convertion(folderPath);
            //}

            this.Convertion(Environment.CurrentDirectory);
        }
        private  void Log(string message)
        {
            textBox1.Text += message + "\r\n";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //this.Convertion();
           
        }
        void Convertion(string folderPath = @"C:\Users\wuqimei\Desktop\Desktop")
        {
            //string folderPath = @"C:\Users\wuqimei\Desktop\Desktop";
            Log("FolderPath:" + folderPath);
            DirectoryInfo directoryInfo = new DirectoryInfo(folderPath);

            int count = 0;
            // 遍历文件夹下的所有文件
            foreach (FileInfo fileInfo in directoryInfo.GetFiles())
            {
                string fileExtension = fileInfo.Extension.ToLower();

                // 只处理LRC和TXT格式文件
                if (fileExtension == ".lrc" || fileExtension == ".txt")
                {
                   



                    //DetectEncoding(fileInfo.FullName);
                    //DetectEncoding2(fileInfo.FullName);
                    Encoding encoding = DetectEncoding3(fileInfo.FullName);

                    if (encoding == null || encoding.EncodingName.Equals("简体中文(GB18030)"))
                    {
                        Log("默认File:" + fileInfo.FullName);
                    }
                    else
                    {
                        // 读取文件内容
                        string fileContent = File.ReadAllText(fileInfo.FullName);

                        // 将文件编码改为ANSI
                        File.WriteAllText(fileInfo.FullName, fileContent, Encoding.GetEncoding("gbk"));
                        Log("转换File:" + fileInfo.FullName);
                    }
                    count++;

                }
            }

            Log("共有"+ count + "个相关文件,编码转换为ANSI:成功");
        }
         Encoding DetectEncoding(string filePath)
        {
            Encoding encoding = null;
            using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {

                byte[] bom = new byte[4];
                fileStream.Read(bom, 0, 4);
                Log(Convert.ToString(bom[0], 16) + " " + Convert.ToString(bom[1], 16) + " " + Convert.ToString(bom[2], 16) + " " + Convert.ToString(bom[3], 16) + " ");
                // 根据文件头部的字节序列判断编码
                if (bom[0] == 0xef && bom[1] == 0xbb && bom[2] == 0xbf)
                {
                    encoding= Encoding.UTF8;
                }
                else if (bom[0] == 0xfe && bom[1] == 0xff)
                {
                    encoding = Encoding.Unicode; // UTF-16 Little Endian
                }
                else if (bom[0] == 0xff && bom[1] == 0xfe)
                {
                    encoding = Encoding.BigEndianUnicode; // UTF-16 Big Endian
                }
                else if (bom[0] == 0 && bom[1] == 0 && bom[2] == 0xfe && bom[3] == 0xff)
                {
                    encoding = Encoding.UTF32;
                }
                else
                {
                    encoding = Encoding.Default;
                }
                Log("文件编码1:" + encoding.EncodingName);
                return encoding;
            }         
        }

        Encoding DetectEncoding2(string filePath)
        {
            Encoding encoding2 = null;
            using (StreamReader reader = new StreamReader(filePath, detectEncodingFromByteOrderMarks: true))
            {
                // 获取读取器的当前编码
                encoding2 = reader.CurrentEncoding;
                Log("文件编码2:"+encoding2.EncodingName);
            }

            return encoding2;
        }
        Encoding DetectEncoding3(string filePath)
        {
            Encoding encoding = null;
            using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                // 创建字符集识别器
                CharsetDetector detector = new CharsetDetector();
                // 检测文件的字符编码
                detector.Feed(fileStream);
                detector.DataEnd();

                if (detector.Charset != null)
                {
                    //Log("文件编码3:" + detector.Charset);
                    encoding = Encoding.GetEncoding(detector.Charset);
                }
                else
                {
                    //Log("无法识别文件的编码");
                }
                return encoding;
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C#中,可以使用以下两种方法将DLL文件打包到一个EXE程序中: 1. 使用ILMerge工具 ILMerge是一个免费的工具,可以将多个.NET程序集合并成一个程序集。你可以将所有的DLL文件EXE文件都放到同一个文件夹中,然后使用ILMerge工具将它们合并成一个EXE文件。 使用ILMerge的命令行如下: ``` ILMerge.exe /out:MyApp.exe MyApp.exe MyDll1.dll MyDll2.dll ``` 其中,/out参数指定合并后的程序集名称,MyApp.exe是主程序集,MyDll1.dll和MyDll2.dll是要合并的程序集。 2. 使用Visual Studio的资源文件 另一种方法是将DLL文件作为资源文件嵌入到EXE程序中。在Visual Studio中,你可以将DLL文件添加到项目中,然后设置它们的Build Action为Embedded Resource。 然后,在代码中,你可以使用Assembly类来访问嵌入的资源。例如: ```csharp using System.Reflection; // 加载嵌入的DLL文件 Assembly assembly = Assembly.LoadFrom("MyApp.exe"); // 获取嵌入的资源流 Stream stream = assembly.GetManifestResourceStream("MyDll1.dll"); // 读取资源流中的数据 byte[] buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); // 将数据加载到内存中 Assembly dll = Assembly.Load(buffer); ``` 这个例子中,MyApp.exe是主程序集,MyDll1.dll是嵌入的DLL文件。使用Assembly.LoadFrom方法加载主程序集,然后使用Assembly.GetManifestResourceStream方法获取嵌入的资源流。最后,使用Assembly.Load方法将数据加载到内存中,这样就可以使用嵌入的DLL文件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值