C#项目使用powershell脚本自动发布签名

  1. 配置项目的发布信息

    在VS里,右键<项目> =>"发布"菜单

  2. 在项目目录里创建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
    }
  3. 运行脚本就能完成发布并签名程序集

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值