Вопрос

How do I export/import VS 2010/2012 settings from the Command Line or using C#? Is it even possible without resorting to GUI Automation?

Это было полезно?

Решение

You can achieve import by providing a settings file with the /ResetSettings argument.

devenv /ResetSettings c:\full\path\to\your\own.vssettings

This works from VS2005 onwards.

Although you can import from the command line, AFAIK there is no export functionality from the commandline. For that you could use a macro:

Sub ExportMacro()
    DTE.ExecuteCommand("Tools.ImportandExportSettings", "/export:own.vssettings")
End Sub 

Or from a commandline c# application (/reference EnvDte)

static void Main(string[] args)
{
     var filename = "own.vssettings";
     var dte = (EnvDTE.DTE) System.Runtime.InteropServices.Marshal.
                                GetActiveObject("VisualStudio.DTE"); // version neutral

     dte.ExecuteCommand("Tools.ImportandExportSettings", "/export:" + filename);
}

To import the from the macro and/or c# program replace /export with /import

Msdn doc

Другие советы

Without resetting, In PowerShell:

function Import-VisualStudioSettingsFile {
    [CmdletBinding()]
    param(
        [string] $FullPathToSettingsFile,
        [string] $DevEnvExe = "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\devenv.exe",
        [int] $SecondsToSleep = 20 # should be enough for most machines
    )

    if(-not (Test-Path $DevEnvExe)) {
        throw "Could not find visual studio at: $DevEnvExe - is it installed?"
    }

    if(-not (Test-Path $FullPathToSettingsFile)) {
        throw "Could not find settings file at: $FullPathToSettingsFile"
    }

    $SettingsStagingFile = "C:\Windows\temp\Settings.vssettings" # must be in a folder without spaces
    Copy-Item $FullPathToSettingsFile $SettingsStagingFile -Force -Confirm:$false

    $Args = "/Command `"Tools.ImportandExportSettings /import:$SettingsStagingFile`""
    Write-Verbose "$Args"
    Write-Host "Setting Tds Options, will take $SecondsToSleep seconds"
    $Process = Start-Process -FilePath $DevEnvExe -ArgumentList $Args -Passthru
    Sleep -Seconds $SecondsToSleep #hack: couldnt find a way to exit when done
    $Process.Kill()
}

Import and export is possible from powershell. To export the current settings to $outFileName:

This requires that visual studio is running. (you can do that from the script by just calling devenv).

First, add enclose the file name in " to allow spaces in file paths:

$filenameEscaped="`"$outFileName`""

$dte = [System.Runtime.InteropServices.Marshal]::GetActiveObject("VisualStudio.DTE.15.0") 
$dte.ExecuteCommand("Tools.ImportandExportSettings", '/export:'+$filenameEscaped)

Optionally, exit:

$dte.ExecuteCommand("File.Exit")

To import, either use the /ResetSettings option to devenv.exe. Or, to import without resetting:`

$dte.ExecuteCommand("Tools.ImportandExportSettings", '/import:'+$filenameEscaped)

This answer is a port of @rene's C# answer. For some reason, I had to specify the exact version of visual studio DTE.15.0.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top