- 配置项目的发布信息
在VS里,右键<项目> =>"发布"菜单

- 在项目目录里创建powershell脚本, 取名为publish.ps1
# 设置严格模式:任何错误立即终止 $ErrorActionPreference = "Stop" Set-StrictMode -Version Latest $buildToDir = "bin\Release\net8.0-windows\publish\win-x64" # ============================== # function SignFiles: 签名文件 # ============================== function SignFiles{ param([string]$Dir,[string]$Filter) $files = Get-ChildItem $Dir -Recurse -Include $Filter -ErrorAction Stop $pfxPath = "..\..\..\sdms.pfx" # pfx证书路径 相对于脚本的位置 $pfxPassword = "123456" $timestampUrl = "http://timestamp.digicert.com" foreach ($file in $files) { & "C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\signtool.exe" sign /fd SHA256 /f $pfxPath /p $pfxPassword /tr $timestampUrl /td SHA256 $file if ($LASTEXITCODE -eq 0) { Write-Host "$file 签名成功" -ForegroundColor Green } else { Write-Error "$file 签名失败" } } } # 获取当前脚本所在目录 $scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Definition Set-Location $scriptDir # 定义发布输出目录 $publishFolder = Join-Path $scriptDir $buildToDir if (Test-Path $publishFolder) { Get-ChildItem -Path $publishFolder -Recurse | Remove-Item -Recurse -Force } # ============================== # Step 1: 发布项目 # ============================== Write-Host "正在发布项目..." -ForegroundColor Cyan dotnet publish "DemoClient.csproj" -p:PublishProfile=FolderProfile ` -p:WarningLevel=0 -p:NoWarn=NU1603 -p:NoWarn=NU1701 -p:NoWarn=NU1608 -p:NoWarn=NU1570 -p:NoWarn=NU1902 ` -p:SuppressTfmSupportBuildWarnings=true ` if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } # ============================== # Step 2: 检测发布输出目录 # ============================== if (-not (Test-Path $publishFolder)) { Write-Error "发布目录不存在: $publishFolder" } # ============================== # Step 3: 清理不需要的文件 # ============================== Write-Host "清理 PDB 和多语言资源..." -ForegroundColor Cyan # 删除所有 .pdb 文件 Get-ChildItem -Path $publishFolder -Filter "*.pdb" -File | Remove-Item # 要删除的语言文件夹列表 $languageFolders = @("cs", "de", "es", "fr", "it", "ja", "ko", "pl", "pt-BR", "ru", "tr") foreach ($lang in $languageFolders) { $folderPath = Join-Path $publishFolder $lang if (Test-Path $folderPath) { Remove-Item -Path $folderPath -Recurse -Force } } # ============================== # Step 5: 签名程序集 # ============================== Write-Host "正在签名程序集..." -ForegroundColor Green SignFiles $publishFolder "Demo.*.dll" SignFiles $publishFolder "Demo.*.exe" Write-Host "✅ 全部完成!" -ForegroundColor Green if (-not ($env:CALLBYBAT -eq 'true')){ Pause } - 运行脚本就能完成发布并签名程序集
C#项目使用powershell脚本自动发布签名
最新推荐文章于 2026-06-21 18:48:43 发布
6244

被折叠的 条评论
为什么被折叠?



