在GAC中注册DLL(CMD或PowerShell)

问题描述 投票:2回答:3

我想在.DLL注册一个GAC。目前我很难证明它已被添加到组件中。

使用命令

C:\ Windows \ System32>%programfiles(x86)%\ Microsoft SDKs \ Windows \ v7.0A \ Bin \ gacutil.exe -i“path \ to \ my \ file.dll”

提示符告诉我程序集已添加到缓存中。

检查时

C:\ Program Files(x86)\ Microsoft SDKs \ Windows \ v7.0A \ Bin> gacutil.exe -l file.dll

它说组件中有0个元素。

通过Powershell命令,我试图像这样添加DLL

PS C:\ WINDOWS \ system32> Add-Type -AssemblyName“System.EnterpriseServices”

PS C:\ WINDOWS \ system32> $ publish = New-Object System.EnterpriseServices.Internal.Publish

PS C:\ WINDOWS \ system32> $ publish.GacInstall(“file.dll”)

我做错了什么?如何将.DLL添加到GAC?最好的方式(对我来说)就是用Powershell做的。

windows powershell dll gac
3个回答
4
投票

请记住以管理员身份运行PowerShell,否则将无法运行。

$dllpath = c:\path\yourdll.dll
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")            
$publish = New-Object System.EnterpriseServices.Internal.Publish            
$publish.GacInstall($dllpath)

在此之后,您可能需要重新启动任何相关的服务或程序,例如:

if (Get-Service "YourService" -ErrorAction SilentlyContinue)
    {
        Stop-Service -Name 'YourService' 
        Start-Service -Name 'YourService' 

    }

或者只是重新启动计算机。


1
投票

我设法用这些Powershell系列来解决它:

Set-location "C:\Temp"
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall("C:\Temp\file.dll")

重新启动PC / Server后,dll将在GAC中注册。谢谢您的帮助! :)


0
投票

使用以下*.batDLL中使用GAC安装或更新GacUtil.exevisual studio developer command prompt

@ECHO OFF
cls
ECHO ------: GAC UPDATER :-----
ECHO -                        -
ECHO  - Updating  %1
gacutil /if %1

将此保存为install.bat,并使用install abcd.dll等参数调用它,%1将接收参数作为文本并将作为gacutil /if abcd.dll服务Gacutil,/if强制安装dll,以便在已经可用的情况下更新它。

如果没有GacUtil.exe,您可以使用下面的powershell脚本

Set-location (Get-Location).Path
[System.Reflection.Assembly]::Load("System.EnterpriseServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
$publish = New-Object System.EnterpriseServices.Internal.Publish
$publish.GacInstall((Get-Location).Path + "\YourFile.dll")

这个powershell脚本获取自身的当前位置并假设YourFile.dll在它旁边,以防dll在某个远程位置更新脚本以用$publish.GacInstall((Get-Location).Path + "\YourFile.dll")替换$publish.GacInstall("path to \YourFile.dll")

将脚本保存为install.ps1remember以用YourFile.dll名称替换dll并将其放在脚本文件旁边然后执行脚本


-1
投票

检查没有扩展名的文件名(.dll)C:\ Program Files(x86)\ Microsoft SDKs \ Windows \ v7.0A \ Bin> gacutil.exe -l文件

© www.soinside.com 2019 - 2024. All rights reserved.