VS.NET Binary Resource To Base64 Macro

This macro was created in response to a thread on the DOTNET-WINFORMS list where someone was wondering how to work with XML resource files independent of the Windows Forms/Controls editors. Basically all the macro does is allow you to select a file which it then opens, converts its contents from a byte array to a Base64 and places the resulting string on the clipboard for you to paste wherever you like.

NOTE: Right now, I can't get the OpenDialog window to pop up on top of the VS.NET main window. This is due to the way they handle the override where no IWin32Window instance is passed and is beyond my control as far as I can tell. If they had an override that took an IntPtr I would be be able to pass the main window's handle, but they don't. So right now you have to ALT+TAB to the dialog, which really sucks. Also, a future version could add an entry to the context menu for files in a project.

To use it, simply open the macro editor, paste it in, then go to Tools -> Options -> Environment -> Keyboard and bind it to whatever key claw you'd like. You can also execute the macro via the command window if you prefer.

Enjoy!

Macro:

Imports EnvDTE
Imports System
Imports System.Windows.Forms
Imports System.Threading

Dim FileContentsBase64 As String 

Public Sub ConvertFileToBase64AndPlaceOnClipboard()
   
Dim OpenDialog As OpenFileDialog = New OpenFileDialog()

    OpenDialog.Title = "Select file to convert to Base64..."
    OpenDialog.Filter = "Icons (*.ico)|*.ico|Cursors (*.cur)|*.cur|All Files|*.*"

    If OpenDialog.ShowDialog() = DialogResult.OK Then
       
Dim FileDataStream As FileStream = File.Open(OpenDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read)

        Try
           
Dim FileLength As Int32 = FileDataStream.Length
           
Dim FileBytes(FileLength - 1) As Byte

            '-- Read the contents of the stream into a byte array
           
FileDataStream.Read(FileBytes, 0, FileLength)

            '-- Close the stream ASAP
           
FileDataStream.Close()

            '-- Convert file contents to base 64
           
FileContentsBase64 = Convert.ToBase64String(FileBytes)

            '-- NOTE: macros run on MTA threads, so we have to delegate the copy logic to an STA thread
           
Dim CopyToClipBoardThread As
           
System.Threading.Thread = New System.Threading.Thread(AddressOf ConvertFileToBase64AndPlaceOnClipboardThreadStart)

            CopyToClipBoardThread.ApartmentState = ApartmentState.STA
            CopyToClipBoardThread.IsBackground =
True
           
CopyToClipBoardThread.Start()

            '-- Wait for copy to happen
           
CopyToClipBoardThread.Join()

        Finally
           
'-- NOTE: always close the stream, double close won't hurt!
           
FileDataStream.Close()
            F
ileContentsBase64 = Nothing
        End Try
   
End If
End Sub

Private Sub ConvertFileToBase64AndPlaceOnClipboardThreadStart()
   
Clipboard.SetDataObject(FileContentsBase64, True)
End Sub