Electron.NET 跨平台打包发布实战:从 Publish Profile 到 electron-builder 的桌面应用分发指南
发布时间:2026/9/28 2:42:59来源:尧图网络
桌面应用跨平台【免费下载链接】Electron.NET:electron: Build cross platform desktop apps with ASP.NET Core (Razor Pages, MVC, Blazor).项目地址https://gitcode.com/gh_mirrors/el/Electron.NET点击查看免费下载本篇指南围绕 Electron.NET 的Package Building打包发布能力展开如何在 Visual Studio 中通过发布系统把基于 ASP.NET Core 或纯 Console 的 .NET 应用打包成可在 Windows、Linux、macOS 上分发的 Electron 安装包。读完本文你将掌握发布配置文件的编写规范、electron-builder 配置的自动生成机制、跨平台构建含 WSL 场景的完整流程以及自定义打包管线所需的ElectronRootDir、ElectronSkipExecCommands、ElectronIntermediatePublishDir等关键 MSBuild 属性。发布总览ASP.NET 与 Console 应用的差异ElectronNET.Core 与 Visual Studio 的发布系统深度集成借助 electron-builder 生成可分发的 Electron 包。整个过程复用了 .NET 的构建系统并自动生成所需的 Electron 配置文件electron-builder.json与package.json开发者几乎不需要手工维护任何 Node.js 侧的构建脚本。发布流程在两种应用形态上略有差异核心区别在于自包含SelfContained运行时的选择应用类型发布方式SelfContained说明ASP.NET 应用文件夹发布FileSystemtrue将 ASP.NET Core 运行时与依赖一并发布目标机器无需安装 .NETConsole 应用文件夹发布FileSystemfalse依赖目标机器已安装对应的 .NET 运行时包体积更小这一差异直接体现在下面两类发布配置文件.pubxml中ASP.NET 项目使用PublishUrlWebPublishMethod而 Console 项目使用PublishDirPublishProtocol。第一步创建发布配置文件Publish Profile在项目根目录下创建Properties/PublishProfiles/文件夹并按目标平台放置对应的.pubxml文件。下面依次给出 ASP.NET 与 Console 应用在四个常见目标Windows x64、Linux x64、macOS Apple Silicon ARM64、macOS Intel x64下的完整配置。ASP.NET 应用发布配置ASP.NET 应用的发布配置文件使用PublishProviderFileSystem、WebPublishMethodFileSystem与_TargetIdFolder组合输出目录由PublishUrl决定且必须设置SelfContainedtrue。win-x64.pubxmlWindows?xml version1.0 encodingutf-8? Project PropertyGroup ConfigurationRelease/Configuration PlatformAny CPU/Platform DeleteExistingFilestrue/DeleteExistingFiles PublishProviderFileSystem/PublishProvider PublishUrlpublish\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\/PublishUrl WebPublishMethodFileSystem/WebPublishMethod _TargetIdFolder/_TargetId TargetFrameworknet10.0/TargetFramework RuntimeIdentifierwin-x64/RuntimeIdentifier ProjectGuid48eff821-2f4d-60cc-aa44-be0f1d6e5f35/ProjectGuid SelfContainedtrue/SelfContained /PropertyGroup /Projectlinux-x64.pubxmlLinux?xml version1.0 encodingutf-8? Project PropertyGroup ConfigurationRelease/Configuration PlatformAny CPU/Platform DeleteExistingFilestrue/DeleteExistingFiles PublishProviderFileSystem/PublishProvider PublishUrlpublish\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\/PublishUrl WebPublishMethodFileSystem/WebPublishMethod _TargetIdFolder/_TargetId TargetFrameworknet10.0/TargetFramework RuntimeIdentifierlinux-x64/RuntimeIdentifier ProjectGuid48eff821-2f4d-60cc-aa44-be0f1d6e5f35/ProjectGuid SelfContainedtrue/SelfContained /PropertyGroup /Projectosx-arm64.pubxmlmacOS Apple Silicon ARM64?xml version1.0 encodingutf-8? Project PropertyGroup ConfigurationRelease/Configuration PlatformAny CPU/Platform DeleteExistingFilestrue/DeleteExistingFiles PublishProviderFileSystem/PublishProvider PublishUrlpublish\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\/PublishUrl WebPublishMethodFileSystem/WebPublishMethod _TargetIdFolder/_TargetId TargetFrameworknet10.0/TargetFramework RuntimeIdentifierosx-arm64/RuntimeIdentifier ProjectGuid48eff821-2f4d-60cc-aa44-be0f1d6e5f35/ProjectGuid SelfContainedtrue/SelfContained /PropertyGroup /Projectosx-x64.pubxmlmacOS Intel x64?xml version1.0 encodingutf-8? Project PropertyGroup ConfigurationRelease/Configuration PlatformAny CPU/Platform DeleteExistingFilestrue/DeleteExistingFiles PublishProviderFileSystem/PublishProvider PublishUrlpublish\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\/PublishUrl WebPublishMethodFileSystem/WebPublishMethod _TargetIdFolder/_TargetId TargetFrameworknet10.0/TargetFramework RuntimeIdentifierosx-x64/RuntimeIdentifier ProjectGuid48eff821-2f4d-60cc-aa44-be0f1d6e5f35/ProjectGuid SelfContainedtrue/SelfContained /PropertyGroup /ProjectConsole 应用发布配置Console 应用的发布配置改用PublishProtocolFileSystem输出目录由PublishDir决定。除SelfContainedfalse外还需显式关闭单文件与 ReadyToRun 特性PublishSingleFilefalse、PublishReadyToRunfalse因为 Electron 打包流程需要的是完整的目录结构而非压缩后的单文件。win-x64.pubxmlWindows?xml version1.0 encodingutf-8? Project PropertyGroup ConfigurationRelease/Configuration PlatformAny CPU/Platform PublishDirpublish\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\/PublishDir PublishProtocolFileSystem/PublishProtocol TargetFrameworknet10.0/TargetFramework RuntimeIdentifierwin-x64/RuntimeIdentifier SelfContainedfalse/SelfContained PublishSingleFilefalse/PublishSingleFile PublishReadyToRunfalse/PublishReadyToRun /PropertyGroup /Projectlinux-x64.pubxmlLinux?xml version1.0 encodingutf-8? Project PropertyGroup ConfigurationRelease/Configuration PlatformAny CPU/Platform PublishDirpublish\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\/PublishDir PublishProtocolFileSystem/PublishProtocol TargetFrameworknet10.0/TargetFramework RuntimeIdentifierlinux-x64/RuntimeIdentifier SelfContainedfalse/SelfContained PublishSingleFilefalse/PublishSingleFile /PropertyGroup /Projectosx-arm64.pubxmlmacOS Apple Silicon ARM64?xml version1.0 encodingutf-8? Project PropertyGroup ConfigurationRelease/Configuration PlatformAny CPU/Platform PublishDirpublish\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\/PublishDir PublishProtocolFileSystem/PublishProtocol TargetFrameworknet10.0/TargetFramework RuntimeIdentifierosx-arm64/RuntimeIdentifier SelfContainedfalse/SelfContained PublishSingleFilefalse/PublishSingleFile /PropertyGroup /Projectosx-x64.pubxmlmacOS Intel x64?xml version1.0 encodingutf-8? Project PropertyGroup ConfigurationRelease/Configuration PlatformAny CPU/Platform PublishDirpublish\$(Configuration)\$(TargetFramework)\$(RuntimeIdentifier)\/PublishDir PublishProtocolFileSystem/PublishProtocol TargetFrameworknet10.0/TargetFramework RuntimeIdentifierosx-x64/RuntimeIdentifier SelfContainedfalse/SelfContained PublishSingleFilefalse/PublishSingleFile /PropertyGroup /Project说明PublishUrl/PublishDir中使用的$(Configuration)、$(TargetFramework)、$(RuntimeIdentifier)会在发布时被自动展开因此同一份模板可复用于不同平台。仓库中 ElectronNET.WebApp 与 ElectronNET.ConsoleApp 分别提供了 ASP.NET 与 Console 的真实发布配置示例可对照参考示例中部分PublishDir写为硬编码路径如publish\Release\net8.0\win-x64实际项目推荐使用上述属性表达式以保持可移植性。第二步配置 electron-builderElectronNET.Core 会在构建/发布时自动在Properties\electron-builder.json位置生成一份默认的electron-builder.json文件你无需从零手写。若该文件已存在例如你放置了自己的自定义配置则不会被覆盖。这一逻辑体现在 ElectronNET.LateImport.targets 的EnsureElectronBuilderJson目标中它检查Properties\$(ElectronBuilderJson)默认electron-builder.json见 ElectronNET.Core.props是否存在若不存在则把包内置的模板复制过去。内置默认模板内容如下src/ElectronNET/build/electron-builder.json{ $schema: https://raw.githubusercontent.com/electron-userland/electron-builder/refs/heads/master/packages/app-builder-lib/scheme.json, compression: maximum, linux: { target: [ tar.xz ], executableArgs: [ --no-sandbox ], artifactName: ${name}-${arch}-${version}.${ext} }, win: { target: [ { target: portable, arch: x64 } ] } }electron-builder 的完整配置选项非常丰富官方文档中有逐项说明本文不再赘述外部链接。下面结合仓库内真实示例补充几个高频场景的写法仓库示例一WebApp / ConsoleApp 的增强配置src/ElectronNET.WebApp/Properties/electron-builder.json 与 src/ElectronNET.ConsoleApp/Properties/electron-builder.json 内容一致展示了图标路径与平台目标的高级配置{ $schema: https://raw.githubusercontent.com/electron-userland/electron-builder/refs/heads/master/packages/app-builder-lib/scheme.json, compression: maximum, linux: { target: [ tar.xz ], executableArgs: [ --no-sandbox ], icon: bin/Assets/icon.png, artifactName: ${name}-${arch}-${version}.${ext} }, win: { target: [ { target: portable, arch: x64 } ], icon: bin/Assets/icon.ico }, mac: { icon: bin/Assets/icon.icns } }仓库示例二Blazor 项目模板的默认配置ElectronNET.Templates 的 Blazor 模板 选择了更贴近正式分发需求的包类型{ compression: maximum, linux: { target: [ AppImage ], executableArgs: [ --no-sandbox ] }, win: { target: [ { target: nsis, arch: x64 } ] }, mac: { target: [ dmg ] }, nsis: { oneClick: true, perMachine: false } }关键配置项速查配置项取值示例作用compressionmaximum/normal/store控制包体压缩级别maximum体积最小但构建最慢win.targetportable/nsis/msiWindows 包类型绿色便携版 / NSIS 安装器 / MSIlinux.targettar.xz/AppImage/deb/rpm/pacmanLinux 包类型可配置多个数组mac.targetdmg/zipmacOS 磁盘镜像 / zip 包executableArgs[--no-sandbox]注入到 Electron 可执行文件的启动参数iconbin/Assets/icon.ico等各平台的应用图标路径发布后位于bin/Assets/下artifactName${name}-${arch}-${version}.${ext}产物文件名模板支持name/arch/version/ext变量nsis.oneClicktrue是否启用 NSIS 一键安装模式第三步从 Visual Studio 发布配置完成后即可通过 Visual Studio 的图形界面触发发布在解决方案资源管理器中右键单击项目选择“发布”Publish选择你的发布配置文件Windows/Linux 等点击“发布”Publish按钮。发布过程中MSBuild 会依次完成构建你的 .NET 应用程序按需复制所有文件安装 npm 依赖npm install运行 electron-builder 生成最终安装包。[!NOTE] 在 Windows 上为 Linux 配置执行发布时Electron.NET 会自动使用WSLWindows Subsystem for Linux执行平台相关的步骤。源码中可见其实现当ElectronPlatform linux且宿主为 Windows 时IsLinuxWsl被置为true见 ElectronNET.LateImport.targets随后 npm/npx 命令会被包装为wsl bash -ic ...执行同文件 L617 与 L676且 Windows 路径会通过wsl wslpath转换为 WSL 内路径L657-L660。发布完成后最终产物位于publish\Release\netx.0\xxx-xxx\其中netx.0为你的目标框架如net10.0xxx-xxx为运行时标识如win-x64、linux-x64、osx-arm64。macOS 构建限制[!NOTE] macOS 构建无法在 Windows 机器上完成因为它们需要 Windows 不支持的符号链接symlink。macOS 构建可以在 Linux 或 macOS 机器上产出。也就是说跨平台构建的完整矩阵是Windows 上可构建 Windows 与 Linux 包Linux 走 WSLmacOS 包只能在 Linux 或 macOS 上构建。在 CI/CD 中规划多平台产物时需要据此分配构建机。自定义打包管线默认打包布局是Electron-Firstelectron-builder 生成 Electron 包.NET 应用被放置在包的resources/bin子目录中。产物结构如下install-root/ MyApp # Electron 可执行文件 resources/ bin/ MyApp # .NET 可执行文件如果你的打包流水线把 .NET 可执行文件放到包根目录、把 Electron 放在子目录中则需要设置ElectronRootDir让 .NET 应用在运行时能定位到 Electron 二进制ElectronRootDirelectron/ElectronRootDir对应的目录布局为install-root/ MyApp # .NET 可执行文件 electron/ MyApp # Electron 可执行文件 resources/ locales/对于由流水线自行提供 npm 依赖的场景例如使用离线缓存或内部镜像ElectronSkipExecCommands可以抑制所有npm/npx调用ElectronIntermediatePublishDir则用于重定向 .NET 应用的中间发布输出目录。这三个属性的默认值与作用来自 ElectronNET.Core.props 与 Configuration属性默认值说明ElectronRootDir../..Electron 二进制相对 .NET 应用目录的位置绝对路径会原样使用。默认../..匹配resources/bin标准布局ElectronSkipExecCommandsfalse设为true时跳过 Electron 构建/发布目标中的所有npm/npx调用ElectronIntermediatePublishDir$(IntermediateOutputPath)PubTmp\覆盖 .NET 应用在组装进 Electron 包之前的中间发布目录源码层面的深入理解ElectronRootDir的默认值../..对应 ElectronRootDirResolver.cs 中的DefaultElectronRootDir常量解析逻辑在Resolve方法中优先读取构建期写入的ElectronNetRuntime.BuildInfo.ElectronRootDir该值由 MSBuild 以AssemblyMetadata形式写入程序集见 ElectronNET.Core.targets为空时回退到../..随后与 .NET 应用目录做Path.Combine后规范化。这同时也被用于判断应用处于打包还是未打包状态见 UnpackagedDetector.cs。ElectronSkipExecCommands的作用点集中在发布阶段ElectronPublishApp目标中npm install --omitdev安装运行时依赖、npm install electron-builder$(ElectronBuilderVersion)安装打包工具与npx electron-builder ...生成安装包三处Exec均带有Condition$(ElectronSkipExecCommands) ! true AND (CopiedFiles-Count()) 0见 ElectronNET.LateImport.targets。ElectronIntermediatePublishDir的接入点在ElectronSetIntermediatePublishDir目标对非 ASP.NET 项目它会先把PublishDir/PublishUrl指向中间目录默认$(IntermediateOutputPath)PubTmp\可用该属性覆盖完成 .NET 发布后再由后续目标移动进 Electron 包结构同文件 L166-L181。发布过程中的 package.json 自动生成electron-builder 仍然要求存在package.json但 ElectronNET.Core 不再接受用户手工维护的electron-manifest.json或自定义package.json—— 它在构建期自动生成应用侧ElectronCreatePackageJson目标基于内置模板 package.template.json通过ReplaceTemplateTask实现见 ReplaceMsBuildPropertiesTask.cs用正则把$(PropertyName)占位符替换为 MSBuild 属性值生成应用的package.json构建侧发布阶段再基于 package.dev.template.json 在发布根目录生成一个开发用的package.json采用 electron-builder 标准的two-package.json 布局应用元数据与运行时依赖放在app\子目录进入app.asarbuild配置放在项目根目录的package.json中L626-L644。模板中electron依赖版本即由$(ElectronVersion)当前仓库默认30.4.0注入electron-builder 版本由$(ElectronBuilderVersion)默认26.0控制见 ElectronNET.Core.props。打包与启动模式的配合打包完成后应用的启动方式由启动模式决定。Electron.NET 支持 Packaged / Unpackaged、Console / ASP.NET、Dotnet-first / Electron-first 的多种组合其中打包场景的两种模式为Electron-First默认直接运行打包产物如MyApp.exeElectron 先启动再拉起 .NET.NET-First以MyApp.exe -dotnetpacked启动.NET 先启动并自行定位 Electron 二进制这正是ElectronRootDir生效的场景。详细说明参见 Startup Methods 与 Configuration。调试打包后的应用可参考 Debugging从旧版发布方式迁移可参考 Migration Guide。核心收益✅原生 VS 集成—— 使用熟悉的发布工作流无需学习额外构建工具 ✅跨平台构建—— 在 Windows 上即可产出 Linux 包自动走 WSL ✅自动配置—— 无需手工搭建 electron-builder 环境配置文件自动生成 ✅多包类型—— NSIS、AppImage、DMG、tar.xz 等一键产出 ✅CI/CD 就绪—— 易于接入各类构建流水线配合ElectronSkipExecCommands可对接私有依赖缓存小结Electron.NET 的打包发布能力把「.NET 构建 → Electron 组装 → 安装包产出」整条链路收拢到 MSBuild 与 Visual Studio 发布系统内。开发者只需准备正确的.pubxml发布配置与Properties/electron-builder.json即可针对 Windows、Linux、macOS 产出可分发安装包对于有特殊布局或离线流水线需求的团队ElectronRootDir、ElectronSkipExecCommands、ElectronIntermediatePublishDir三个属性提供了充分的定制空间。赞分享桌面应用跨平台【免费下载链接】Electron.NET:electron: Build cross platform desktop apps with ASP.NET Core (Razor Pages, MVC, Blazor).项目地址https://gitcode.com/gh_mirrors/el/Electron.NET点击查看免费下载相关推荐终极指南跨平台Electron应用打包分发的完整流程终极指南跨平台Electron应用打包分发的完整流程 Upscayl作为一款开源AI图像放大工具采用Linux优先的开发理念通过Electron框架实现了桌面应用AI 应用计算机视觉图像处理GraphQL Playground Electron 桌面版开发调试与跨平台打包发布实战指南GraphQL Playground Electron 桌面版开发调试与跨平台打包发布实战指南 导读 本文围绕 graphql playground elec开发工具后端API设计Electron.NET实战指南用C打造跨平台桌面应用快速入门指南 想用熟悉的C 语言开发桌面应用吗Electron.NET正是你需要的利器这个项目巧妙地将.NET Core与Electron框架结合让你既能桌面应用跨平台上一篇MoneyPrinterTurbo如何用AI技术5分钟创作专业级短视频下一篇终极指南Canvas iOS动画库核心组件CSAnimationView与CSBlurView深度解析创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
网站建设高端定制企业官网