如何在 Visual Studio 2015/2017 中备份窗口布局?
导出设置不会导出所有布局(仅导出当前布局)。我知道 AppData/Roaming/Microsoft/VisualStudio 下有一个名为 WindowLayouts 的目录,但它也只存储当前布局。
我想备份所有设置,但不导出每个布局的设置。Visual
Studio 将它们存储在哪里?
答案1
Visual Studio 2017+ 将并排安装的窗口布局信息保存在 下[LOCALAPPDATA]/Microsoft/VisualStudio/{VisualStudioInstanceID}
。
我编写了一个脚本,循环遍历所有实例,通过vswhere.exe
,并备份和恢复(如果感兴趣请与我联系!)这些设置。
笔记: 请确保你有
vswhere.exe
PATH
执行脚本时,它会下载并存在于您当前的环境变量中。
备份
backup-windowlayoutinfolist.ps1:
#!/usr/bin/env pwsh
[CmdletBinding()]
param()
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Import modules
try { Import-Module SmartLogging } catch { Install-Module SmartLogging -Scope CurrentUser -Force; Import-Module SmartLogging }
try { Import-Module Execution } catch { Install-Module Execution -Scope CurrentUser -Force; Import-Module Execution }
Set-ScriptArgs $MyInvocation.BoundParameters $MyInvocation.UnboundArguments
function Backup-VisualStudioWindowLayouts([string] $vsLocalAppDataDir, [string] $versionAndInsanceId) {
Log trace "vsLocalAppDataDir: $vsLocalAppDataDir"
Log trace "versionAndInsanceId: $versionAndInsanceId"
$sourceFilePath = Join-Path $vsLocalAppDataDir 'ApplicationPrivateSettings.xml'
$destinationDir = Join-Path $PSScriptRoot "config/$versionAndInsanceId"
$destinationFilePath = Join-Path $destinationDir 'WindowLayoutInfoList.xml'
if (-not (Test-Path $destinationDir -PathType Container) ) {
New-Item -Path $destinationDir -ItemType Directory > $null
}
[xml]$xml = Get-Content -Path $SourceFilePath
$windowLayoutInfoListXml = $xml.SelectSingleNode($XpathSearchPattern)
$windowLayoutInfoListXml.OuterXml | Set-Content -Path $DestinationFilePath -Force
Log info "Saved window layout to '$DestinationFilePath'"
}
try {
$XpathSearchPattern = "/content/indexed/collection[@name='Microsoft.VisualStudio.Platform.WindowManagement.Layouts.WindowLayoutInfoList']"
$Instances = Start-NativeExecution vswhere.exe -prerelease -products * -legacy -format json | ConvertFrom-Json
foreach ($instance in $Instances) {
if (-not ('installationName' -in $instance.PSObject.Properties.Name)) {
$instance | Add-Member -NotePropertyName 'installationName' -NotePropertyValue 'n/a'
}
$buildVersion = [Version]::new($instance.installationVersion)
$versionAndInsanceId = "$($buildVersion.Major).0_$($instance.instanceId)"
$vsLocalAppDataDir = Join-Path $env:LOCALAPPDATA (Join-Path 'Microsoft/VisualStudio' $versionAndInsanceId)
if (Test-Path $vsLocalAppDataDir) {
Log info "Backup window layouts for Visual Studio Version $($instance.installationVersion) ($($instance.installationName))..."
Backup-VisualStudioWindowLayouts -vsLocalAppDataDir $vsLocalAppDataDir -versionAndInsanceId $versionAndInsanceId
Log info "Backup window layouts for Visual Studio Version $($instance.installationVersion) ($($instance.installationName))... Done."
}
}
Log info 'Successfully'
Exit-WithAndWaitOnExplorer 0
} catch {
Log error "Something went wrong: $_"
Log trace "Exception: $($_.Exception)"
Log trace "StackTrace: $($_.ScriptStackTrace)"
Exit-WithAndWaitOnExplorer 1
}