傳統用 gacutil /i MyAssembly.dll,但 gacutil 屬於 Windows SDK,非 .NET Framework 內建,純 .NET 環境可能找不到。
[Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices") | Out-Null
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("C:\Full\Path\To\MyAssembly.dll")
System.EnterpriseServices.Internal.Publish 類別封裝 GAC 操作GacInstall(path) 將指定組件安裝至全域組件快取GacRemove(assemblyName) 移除組件RegAsm 相似功能 (註冊 COM Interop)
param(
[Parameter(Mandatory=$true)]
[string]$DllPath
)
if (-not (Test-Path $DllPath)) {
Write-Error "找不到檔案: $DllPath"
exit 1
}
try {
[Reflection.Assembly]::LoadWithPartialName("System.EnterpriseServices") | Out-Null
$pub = New-Object System.EnterpriseServices.Internal.Publish
$pub.GacInstall($DllPath)
Write-Host "成功安裝至 GAC: $DllPath" -ForegroundColor Green
}
catch {
Write-Error "安裝失敗: $_"
exit 1
}
儲存為 Install-Gac.ps1,使用:.\Install-Gac.ps1 -DllPath ".\MyLib.dll"