Follow @RoyOsherove on Twitter

Export and import VS.NET's text editor's color and font settings

I like customizing the way my VS.NET text editor looks and feels. I like dabbling with the colors and settings of the editor until everything is just as I like it. One caveat – If I move to a different computer – all the settings are gone, and I could find no way of taking them with me.

So – I created this handy little macro module, which allows you to :

·         Save your current colors and fonts settings in a text file

·         Load colors and settings from a text file

 

Simple huh?

I’ve only tested this with vs.net 2002, so I’d like to know if it works on 2003 as well…

Here’s the code. To use it, just copy and paste it all in a new module in your vs.net macros(ALT+F11).

I'll try to turn this into an easier t use addin(will be a nice learning exercise..)

Update:

I'll have the Add-in ready by the end of the day :) it's pretty cool!


Imports EnvDTE

Imports System.IO

Imports System.Text

Imports System

 

Public Module Settings

    '-------------------------------------------------

    'saves the current color and font settings of the text editor to a named text file

    Public Sub SaveColorSettings()

        Dim sb As New StringBuilder()

        Dim clr As [Property]

        Dim ci As ColorableItems

        Dim sFile As String = GetFileName()

        If sFile = "" Then Exit Sub

 

clr = _

DTE.Properties("FontsAndColors","TextEditor").Item(_

"FontsAndColorsItems")

 

        For Each ci In clr.Object

            sb.Append(ci.Name & vbTab & _

ci.Foreground.ToString() & vbTab & _

ci.Bold.ToString() & vbTab & _

ci.Background.ToString() & vbNewLine)

        Next

 

        Dim write As TextWriter = File.CreateText(sFile)

        write.Write(sb.ToString())

        write.Close()

 

        MsgBox(sFile & " created!", MsgBoxStyle.Information)

    End Sub

    '--------------------------------------------

    'Loads color and font settings of the

‘text editor from a named text file

    Public Sub LoadColorSettings()

        Dim sFile As String = GetFileName()

        If sFile = "" Then Exit Sub

 

        Dim reader As TextReader = File.OpenText(sFile)

        Dim sLine As String

        Try

            Do

                sLine = reader.ReadLine()

                ParseLine(sLine)

            Loop While sLine.Length > 0

        Catch e As Exception

        End Try

 

        MsgBox("settings loaded successfully!")

    End Sub

    '---------------------------------------------------

    Private Function DefaultSettingsFileName() As String

        Return Path.Combine(Environment.CurrentDirectory, "EditorSettings.txt")

    End Function

    '------------------------------------------------------

    Private Sub ParseLine(ByVal sLine As String)

        Dim clr As [Property]

        Dim ci As ColorableItems

        Dim arr As String()

 

        Try

            arr = Split(sLine, vbTab)

 

            clr = DTE.Properties("FontsAndColors", "TextEditor").Item("FontsAndColorsItems")

            ci = clr.Object(arr(0))

 

            ci.Foreground = UInt32.Parse(arr(1).ToString())

            ci.Bold = Boolean.Parse(arr(2).ToString())

            ci.Background = UInt32.Parse(arr(3).ToString())

 

        Catch e As Exception

        End Try

 

    End Sub

    '------------------------------------------------------

    Private Function GetFileName() As String

        Dim sFile As String = InputBox( _

"Enter the file name to create/load from", _

"Settings file name", DefaultSettingsFileName())

        Return sFile

    End Function

    '-------------------------------------------------

End Module

 

What's missing in today's hottest languages?

How's this for less eye strain