How to backup Visual Studio 2015/2017 layout?

5

0

How do I backup window layouts in Visual Studio 2015/2017?

Exporting settings does not export all layouts (just current one). I know there is a directory under AppData/Roaming/Microsoft/VisualStudio called WindowLayouts but that again stores only current layout.

I want to backup all of them without exporting settings for each layout.
Where does Visual Studio store them?

UfoXp

Posted 2017-12-10T17:49:36.853

Reputation: 211

Which layout do you want to backup? In VS2017, it can export every settings. Did you check all the items in export settings window? – Biswapriyo – 2017-12-10T20:45:44.653

I’ve just tried adding new layout using Window > Save Window Layout, exporting settings, removing layout and importing settings that I exported a moment before. New layout is NOT restored to the list of layouts. – UfoXp – 2017-12-10T20:59:35.947

Did you manage to find a solution for this? With VS 2017 this seems to be the same issue, window layouts can't be exported and imported (despite being able to export "Window Layouts" under General settings, but apparently it's not the same). – Piedone – 2019-03-18T19:54:02.430

Answers

1

Visual Studio 2017+ saved the window layout information for side-by-side installations under [LOCALAPPDATA]/Microsoft/VisualStudio/{VisualStudioInstanceID}.

I wrote a script which loops through all instances, via vswhere.exe, and backup and restore (ping me if interests!) these settings.

Note: Please make sure you have vswhere.exe downloaded and available in your current PATH environment variable when executing the script.

Backup

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
}

Manuel

Posted 2017-12-10T17:49:36.853

Reputation: 11

Some more explanation would improve your answer. – zx485 – 2020-02-15T23:26:30.753