简体   繁体   English

在 C# 表单应用程序中包含 .dll 文件

[英]Including .dll files in C# Forms Application

I am using Visual Studio 2019 to build a C# Forms application.我正在使用 Visual Studio 2019 构建 C# 表单应用程序。 When publishing an .exe file from a Console application, I have the option to include all the .dll files into the .exe, meaning that users of my application only need to download one file.从控制台应用程序发布 .exe 文件时,我可以选择将所有 .dll 文件包含到 .exe 中,这意味着我的应用程序用户只需要下载一个文件。 However, when publishing an .exe from a Forms application, I'm forced to include all of these .dll files independently:但是,当从 Forms 应用程序发布 .exe 时,我被迫独立包含所有这些 .dll 文件:

View of folder with .dll files查看包含 .dll 文件的文件夹

Is there a way to include these .dll files inside of the .exe so that I only have to send someone the .exe file for them to be able to run it, similar to how it's possible for Console applications?有没有办法将这些 .dll 文件包含在 .exe 中,这样我只需要将 .exe 文件发送给某人,他们就可以运行它,类似于控制台应用程序的可能方式?

EDIT: I've discovered that I was in .NET Core and should've been in .NET Framework.编辑:我发现我在 .NET Core 中并且应该在 .NET Framework 中。 I'm relatively new to C# and wasn't aware of the differences.我对 C# 比较陌生,不知道这些差异。 This issue is now resolved!这个问题现在已经解决了! If anyone runs into this problem, just copy-paste your code over to .NET Framework and listen to the instructions in the description.如果有人遇到此问题,只需将您的代码复制粘贴到 .NET Framework 并听取说明中的说明。

You need to change the configuration settings during the build step to be "self-contained":您需要在构建步骤期间将配置设置更改为“自包含”:

Using dotnet cli使用dotnet cli

dotnet publish -r win-x64 -c Release --self-contained

Using Visual Studio使用 Visual Studio

  1. Add <PublishSingleFile>true</PublishSingleFile> to your project file.<PublishSingleFile>true</PublishSingleFile>到您的项目文件中。

  2. On the Solution Explorer pane, right-click on the project you want to publish.解决方案资源管理器窗格中,右键单击要发布的项目。 Select Publish .选择发布

    解决方案浏览器

    If you don't already have a publishing profile, follow the instructions to create one and choose the Folder target-type.如果您还没有发布配置文件,请按照说明创建一个并选择文件夹目标类型。

  3. Choose Edit .选择编辑

    编辑

  4. In the Profile settings dialog, set the following options:配置文件设置对话框中,设置以下选项:

    • Set Deployment mode to Self-contained or Framework-dependent .部署模式设置为Self-containedFramework-dependent
    • Set Target runtime to the platform you want to publish to.目标运行时设置为要发布到的平台。 (Must be something other than Portable .) (必须是Portable以外的东西。)
    • Select Produce single file .选择生成单个文件

    Choose Save to save the settings and return to the Publish dialog.选择保存以保存设置并返回到发布对话框。

    配置文件设置

  5. Choose Publish to publish your app as a single file.选择发布以将您的应用程序发布为单个文件。

For more information, see Visual Studio Docs: Single file deployment and executable .有关详细信息,请参阅Visual Studio 文档:单个文件部署和可执行文件


Duplicate of Embedding DLLs in a compiled executable在已编译的可执行文件嵌入 DLL 的重复项

I don't think compile all files into one is a good idea.我不认为将所有文件编译成一个是个好主意。 If you want to have single file for downloading, you can package them into a self-extract zip format (winRAR, winzip or 7zip).如果您希望有单个文件供下载,您可以将它们打包成自解压 zip 格式(winRAR、winzip 或 7zip)。 Good luck~~祝你好运~~

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM