如何从命令行或使用 C# 导出/导入 VS 2010/2012 设置?甚至可以不借助 GUI 自动化吗?
您可以通过提供带有 /ResetSettings
参数的设置文件来实现
import。
devenv /ResetSettings c:\full\path\to\your\own.vssettings
这从 VS2005 开始工作。
虽然您可以从命令行import,但据我所知,命令行没有export功能。为此,您可以使用宏:
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
无需重置,在 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()
}
可以从powershell导入和导出。将当前设置导出到
$outFileName
:
这需要运行visual studio。 (您可以通过调用 devenv 从脚本中做到这一点)。
首先,在
"
中添加包含文件名以允许文件路径中的空格:
$filenameEscaped="`"$outFileName`""
$dte = [System.Runtime.InteropServices.Marshal]::GetActiveObject("VisualStudio.DTE.15.0")
$dte.ExecuteCommand("Tools.ImportandExportSettings", '/export:'+$filenameEscaped)
可选,退出:
$dte.ExecuteCommand("File.Exit")
要 import,请使用 devenv.exe 的
/ResetSettings
选项。或者,在不重置的情况下导入:`
$dte.ExecuteCommand("Tools.ImportandExportSettings", '/import:'+$filenameEscaped)
这个答案是@rene 的 C# 答案的一部分。出于某种原因,我必须指定 visual studio
DTE.15.0
的确切版本。
在powershell中,这将启动visual studio(devenv.exe),并执行命令。在这里它将所有设置导出到给定路径:
cd C:\Program Files\Microsoft Visual Studio�2\Community\Common7\IDE .\devenv.exe /Command "Tools.ImportandExportSettings /export:c:/temp/mysettings.vssettings"