vsto开发随笔

VSTOInstaller.exe工具

VSTOInstaller.exe是Office解决方案的安装程序工具,可以用它来安装和卸载Office解决方案。

安装:

1
.\VSTOInstaller.exe /I http://yourdomain.com/addin.vsto

使用/s可静默执行:

1
.\VSTOInstaller.exe /U http://yourdomain.com/addin.vsto /S

它的位置默认在%commonprogramfiles%\microsoft shared\VSTO\10.0\VSTOInstaller.exe

注意,是%commonprogramfiles%,不是%commonprogramfiles(x86)%

如果错误的使用了%commonprogramfiles(x86)%,会提示错误:

其实换回%commonprogramfiles%就可以了,但已经花了时间去查找解决方案就还是记录一下。

安装vstor_redist.exe就可正常运行%commonprogramfiles(x86)%下的VSTOInstaller。使用命令.\vstor_redist.exe /q /norestart可静默安装。

安装时的安全认证

使用VSTOInstaller.exe安装office解决方案时,需要解决方案通过安全认证才能安装。

最官方的安全认证方式当然是注册一个微软认证的开发者账户,使用开发者账户来发布应用。但咱们做的vsto插件是内部使用,所以跳过这个最麻烦的方案。

除开官方答案,还有两种方式来通过:

  • 打开信任提示
  • 添加签名到受信任

打开信任提示

我们可以让用户开启信任提示,这样就可以安装非认证应用。

可以使用脚本来实现,不过请注意,修改注册表需要管理员权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function Register-Registry {
$path = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\.NETFramework\Security\TrustManager"
$id = "PromptingLevel"
New-Item -Path $path -Name $id -Force | Out-Null

$registryPath = "{0}\{1}" -f $path, $id
New-ItemProperty -Path $registryPath -Name "MyComputer" -Value "Enabled" -PropertyType String -Force | Out-Null
New-ItemProperty -Path $registryPath -Name "LocalIntranet" -Value "Enabled" -PropertyType String -Force | Out-Null
New-ItemProperty -Path $registryPath -Name "TrustedSites" -Value "Enabled" -PropertyType String -Force | Out-Null
New-ItemProperty -Path $registryPath -Name "Internet" -Value "Enabled" -PropertyType String -Force | Out-Null
New-ItemProperty -Path $registryPath -Name "UntrustedSites" -Value "Enabled" -PropertyType String -Force | Out-Null

$path = "HKLM:\SOFTWARE\Microsoft\.NETFramework\Security\TrustManager"
$id = "PromptingLevel"
New-Item -Path $path -Name $id -Force | Out-Null

$registryPath = "{0}\{1}" -f $path, $id
New-ItemProperty -Path $registryPath -Name "MyComputer" -Value "Enabled" -PropertyType String -Force | Out-Null
New-ItemProperty -Path $registryPath -Name "LocalIntranet" -Value "Enabled" -PropertyType String -Force | Out-Null
New-ItemProperty -Path $registryPath -Name "TrustedSites" -Value "Enabled" -PropertyType String -Force | Out-Null
New-ItemProperty -Path $registryPath -Name "Internet" -Value "Enabled" -PropertyType String -Force | Out-Null
New-ItemProperty -Path $registryPath -Name "UntrustedSites" -Value "Enabled" -PropertyType String -Force | Out-Null
}

添加签名到受信任

将应用签名证书的公钥给用户,让用户添加至受信任区域。

这个方案比上面的打开信任提示要好,打开提示后用户还是需要每次都点一下确认,而信任签名后所有操作都可以是静默无感的。

操作方式可参考certmgr.exe(证书管理器工具)

自更新

自更新也有两种方式,

  • ClickOnce自带的更新
  • 在程序中更新

ClickOnce自带的更新

我们在发布时,新更新频率设为每次运行自定义项时进行检查就搞定。

不管是打开信任提示还是添加签名到受信任,都可以使用这种方式更新。

在程序中更新

想要在程序中更新,用打开信任提示的方案就行不通了,ApplicationDeployment.CheckForUpdateAsync会抛出异常User has refused to grant required permissions to the application.。只能使用添加签名到信任的方案。

但是,在使用ApplicationDeployment.UpdateAsync更新后,ApplicationDeployment.IsNetworkDeployed变成了false。

网上有回复说要需要将vsto的网址加入信任网站名单

可使用管理员权限运行ps脚本:

1
2
3
4
5
6
7
8
9
10
11
function Add-TrustSite {
$UserRegPath = "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains"
$DomainName = "yourdomain.com" # 注意这里只能是一级域名
if (-not (Test-Path -Path "$UserRegPath\$DomainName"))
{
$null = New-Item -Path "$UserRegPath\$DomainName"
}

Set-ItemProperty -Path "$UserRegPath\$DomainName" -Name http -Value 2 -Type DWord
Set-ItemProperty -Path "$UserRegPath\$DomainName" -Name https -Value 2 -Type DWord
}

然而试下并没有什么卵用,该false还是false。

终于找到一个相同问题的提问,里面的最佳回复指向了微软的一个问答博客

长求总就是,不要在程序中使用ClickOnce Api比如ApplicationDeployment.UpdateAsync来进行程序中更新,但可以使用VSTOInstaller.exe。参见这篇博客