从命令行导出/导入Visual Studio设置

eblbsuwk  于 2023-04-22  发布在  其他
关注(0)|答案(4)|浏览(131)

如何从命令行或使用C#导出/导入VS 2010/2012设置?是否可以不使用GUI自动化?

hgqdbh6s

hgqdbh6s1#

您可以通过提供一个带有/ResetSettings参数的设置文件来实现导入

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

从VS 2005开始就可以了。
虽然可以从命令行导入,但AFAIK命令行没有导出功能。为此,您可以使用宏:

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

或从命令行c#应用程序(/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);
}

要从宏和/或C#程序导入,请将 /export 替换为 /import
Msdn doc

ar5n3qh5

ar5n3qh52#

不重置,在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()
}
xytpbqjk

xytpbqjk3#

可以从powershell导入导出,要将当前设置导出到$outFileName
这要求VisualStudio正在运行。(您可以通过调用devenv从脚本中完成此操作)。
首先,添加将文件名括在"中以允许文件路径中有空格:

$filenameEscaped="`"$outFileName`""

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

或者,退出:

$dte.ExecuteCommand("File.Exit")

导入,可以使用devenv.exe的/ResetSettings选项,或者不重置导入:`

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

这个答案是@rene的C#答案的一个端口。出于某种原因,我必须指定visual studio DTE.15.0的确切版本。

hk8txs48

hk8txs484#

在powershell中,这将启动visual studio(devenv.exe),并执行命令。在这里,它将所有设置导出到给定的路径:

cd C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE

.\devenv.exe /Command "Tools.ImportandExportSettings /export:c:/temp/mysettings.vssettings"

相关问题