Writing Visual Studio settings in the extension does not remain

In my extension, which I write for Visual Studio 2015, I want to change the tab size and the indent size, because at work we have a different setting when I develop an open source project (company history dating back to our C period). In my command class, I wrote the following code:

private const string CollectionPath = @"Text Editor\CSharp"; private void MenuItemCallback(object sender, EventArgs e) { var settingsManager = new ShellSettingsManager(ServiceProvider); var settingsStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings); var tabSize = settingsStore.GetInt32(CollectionPath, "Tab Size", -1); var indentSize = settingsStore.GetInt32(CollectionPath, "Indent Size", -1); if (tabSize != -1 && indentSize != -1) { settingsStore.SetInt32(CollectionPath, "Tab Size", 2); settingsStore.SetInt32(CollectionPath, "Indent Size", 2); } } 

When testing in an experimental bush, it changes it during the passage of the method, but when you open the "Parameters" dialog, it retains the original values. When you debug again, the values ​​remain original.

What did I forget or did wrong?

+6
source share
2 answers

Direct access to Visual Studio options using the Properties function in the EnvDTE assembly.

 private void ChangeTabs(DTE vs, int newTabSize, int newIndentSize) { var cSharp = vs.Properties["TextEditor", "CSharp"]; EnvDTE.Property lTabSize = cSharp.Item("TabSize"); EnvDTE.Property lIndentSize = cSharp.Item("IndentSize"); lTabSize.Value = newTabSize; lIndentSize.Value = newIndentSize; } private void ChangeSettings() { DTE vs = (DTE)GetService(typeof(DTE)); ChangeTabs(vs, 3, 3); } 

For reference: Manage parameter settings

+4
source

To be complete. This is the correct answer:

In the constructor you need to add

 _dte2 = (DTE2) ServiceProvider.GetService(typeof (DTE)); 

And with the command, it looks like

  _dte2.Properties["TextEditor", "CSharp"].Item("TabSize").Value = 2; _dte2.Properties["TextEditor", "CSharp"].Item("IndentSize").Value = 2; _dte2.Commands.Raise(VSConstants.CMDSETID.StandardCommandSet2K_string, (int)VSConstants.VSStd2KCmdID.FORMATDOCUMENT, null, null); 

There is a problem that changed settings are overridden by default when restarting Visual Studio.

+1
source

All Articles