Import/Export VS settings programmatically?

2019-06-26 03:29发布

问题:

Is there a way to import/export Visual Studio settings via a Visual Studio Extension, i.e., via the Visual Studio API?

I want to export some specific settings to a local file, just like the VS Import and Export Settings Wizard does, however, without UI interaction of course.

回答1:

There is a Visual Studio Command named Tools.ImportandExportSettings

You can execute the Command with DTE2.ExecuteCommand

Import/Export example:

dte2.ExecuteCommand("Tools.ImportandExportSettings", "/export:\"C:/temp/setttings.vssettings\"")
dte2.ExecuteCommand("Tools.ImportandExportSettings", "/import:\"C:/temp/settings.vssettings\"")


回答2:

You could probably also use Roslyn:

var componentModel = (IComponentModel) Package.GetGlobalService(typeof(SComponentModel));
var visualStudioWorkspace = componentModel.GetService<VisualStudioWorkspace>();
visualStudioWorkspace.Options = visualStudioWorkspace.Options.WithChangedOption(CSharpFormattingOptions.NewLineForElse, false);

I haven't tried it, but the api suggests that you can indeed change global options via roslyn.