Click here to Skip to main content
15,885,010 members
Articles / Programming Languages / Visual Basic

Visual Studio Backup Macro

Rate me:
Please Sign up or sign in to vote.
4.70/5 (10 votes)
25 Sep 2010Public Domain2 min read 46.1K   394   20   19
Macro to backup the contents of a solution with a simple click

Introduction

Have you ever screwed up the code of your project and needed to revert to a past version?

Unfortunately, Visual Studio doesn't have a backup option. If you need to do so, you have to manually copy your solution folder to another folder or media.

Tired of doing that, I decided to make a backup macro: my goal was to add a context menu option to Solution Explorer, to automatize the process of copying its files to the destination location.

Background

As described above, this adds an item to the solution context menu (named 'Make backup').
When this option is selected, it cleans the solution's temporary files, then copies its folder contents to a destination with the following format:

F:\BACKUP\Solution Name\month-day-year hour:minute:second

The F:\BACKUP folder name is hardcoded in the macro and must be changed to your backup destination folder.

Using the Code

First, you have to add the macro code to Visual Studio IDE. This is easily done from the Tools->Macros->Macro IDE menu.

Now, as stated above, you have to change the destination folder, which is hard coded in the macro as an optional parameter. I use F:\BACKUP, which is in an external USB Hard Drive.

Then, you need to create the context menu item that calls the macro:

  • Open Tools->Customize menu
  • Select Commands tab
  • Check "Context Menu" radio button, then select "Project and Solution Context Menu | Solution"
  • Click on Add Command button
  • Select Macros on the menu of the left
  • Select the created macro from the menu on the right (Macros.MyMacros.Backup.Make) and click OK
  • Now the option is added, but with the name of the macro. I changed it to "Make backup" from the Change Selection button

NOTE: In Visual Studio 2008, things are a little different. To add a macro to a context menu, you have to open the Context Menu toolbar and drag the macro from the list to the toolbar.

And you are done, now you can click in your solution and select the new item in the context menu to automatically make a backup of it.

Conclusion

I hope you find this code as useful as I do.

I've tested this macro in Visual Studio 2008 and 2010 and it works perfectly in both, I think that it should work in 2005 too.

Source Code

VB.NET
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports System.IO

Public Module Backup
    Public Sub Make(Optional ByVal backupdir As String = "F:\BACKUP")
        Dim SolutionExplorer As UIHierarchy
        Dim Item As UIHierarchyItem
        Dim Solution As EnvDTE.SolutionClass
        Dim SourceFolder As String, TargetFolder As String
        If Not Directory.Exists(backupdir) Then
            MsgBox("Cannot access destination folder(" + backupdir + ")", _
		MsgBoxStyle.Critical, "Backup copy")
            Exit Sub
        End If

        SolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
        For Each Item In SolutionExplorer.SelectedItems
            Solution = CType(Item.Object, EnvDTE.SolutionClass)
            If Solution.SolutionBuild.BuildState = _
			vsBuildState.vsBuildStateInProgress Then
                MsgBox("Cannot make backup while building solution", _
			MsgBoxStyle.Critical, "Backup copy")
                Exit Sub
            End If
            Solution.SolutionBuild.Clean(True)
            SourceFolder = Path.GetDirectoryName(Solution.FullName)
            TargetFolder = Path.Combine(backupdir, Solution.Properties.Item("Name").Value)
            Directory.CreateDirectory(TargetFolder)
            TargetFolder = TargetFolder + "\" + Format(Now, "MM-dd-yy HH_MM_ss")
            If Not CopyDir(SourceFolder, TargetFolder) Then
                MsgBox("Destination folder already exists. Backup aborted", _
			MsgBoxStyle.Critical, "Backup copy")
                Exit Sub
            End If
            MsgBox("Backup done successfully", MsgBoxStyle.Information, "Backup copy")
        Next
    End Sub

    Private Function CopyDir(ByVal source As String, ByVal target As String) As Boolean
        Dim diSource As DirectoryInfo
        Dim diTarget As DirectoryInfo

        diSource = New DirectoryInfo(source)
        diTarget = New DirectoryInfo(target)
        Return CopyDir(diSource, diTarget)
    End Function
    Private Function CopyDir(ByVal source As DirectoryInfo, _
	ByVal target As DirectoryInfo, Optional ByVal AllowCreate As Boolean = False) _
		As Boolean
        Dim fi As FileInfo
        Dim di As DirectoryInfo, td As DirectoryInfo

        If AllowCreate Then
            If Not Directory.Exists(target.FullName) _
		Then Directory.CreateDirectory(target.FullName)
        Else
            If Directory.Exists(target.FullName) Then Return False
        End If

        Directory.CreateDirectory(target.FullName)
        For Each fi In source.GetFiles()
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), True)
        Next

        For Each di In source.GetDirectories()
            td = target.CreateSubdirectory(di.Name)
            CopyDir(di, td, True)
        Next
        Return True
    End Function
End Module

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Engineer
Argentina Argentina
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionError running on VS 2010 SP1 Pin
George Eckert19-Oct-12 11:39
George Eckert19-Oct-12 11:39 
QuestionDoes not work in vs2008 Pin
gallzzy29-Nov-11 10:03
gallzzy29-Nov-11 10:03 
AnswerRe: Does not work in vs2008 Pin
gallzzy30-Nov-11 12:00
gallzzy30-Nov-11 12:00 
GeneralBackup works great Pin
RAPDev24-Jan-11 6:03
RAPDev24-Jan-11 6:03 
GeneralMy vote of 4 Pin
vbmike11-Oct-10 12:13
vbmike11-Oct-10 12:13 
GeneralNice Tool Pin
Member 459897928-Sep-10 18:21
Member 459897928-Sep-10 18:21 
GeneralRe: Nice Tool Pin
ja_benetti29-Sep-10 4:02
ja_benetti29-Sep-10 4:02 
QuestionSource Control? Pin
Pete BSC28-Sep-10 7:57
Pete BSC28-Sep-10 7:57 
AnswerRe: Source Control? Pin
masmozzi1-Oct-10 1:20
masmozzi1-Oct-10 1:20 
GeneralMy vote of 1 Pin
J.Discount28-Sep-10 1:32
J.Discount28-Sep-10 1:32 
General5 for you Pin
A Praveen Kumar26-Sep-10 23:55
A Praveen Kumar26-Sep-10 23:55 
GeneralMy vote of 5 Pin
A Praveen Kumar26-Sep-10 23:53
A Praveen Kumar26-Sep-10 23:53 
GeneralMy vote of 4 Pin
MadZookeeper25-Sep-10 15:27
MadZookeeper25-Sep-10 15:27 
GeneralRe: My vote of 4 Pin
ja_benetti25-Sep-10 16:49
ja_benetti25-Sep-10 16:49 
GeneralGreat Option! however... Pin
Ashok Gowtham25-Sep-10 2:50
Ashok Gowtham25-Sep-10 2:50 
GeneralRe: Great Option! however... Pin
ja_benetti25-Sep-10 8:51
ja_benetti25-Sep-10 8:51 
GeneralRe: Great Option! however... Pin
ja_benetti25-Sep-10 11:46
ja_benetti25-Sep-10 11:46 
GeneralThe problem Pin
Ashok Gowtham8-Oct-10 13:55
Ashok Gowtham8-Oct-10 13:55 
GeneralMy vote of 5 Pin
Adem Gashi24-Sep-10 11:07
Adem Gashi24-Sep-10 11:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.