Visual Studio macro to group files

Visual Studio has the concept of grouping files together a feature used mainly from code generation tools to groups generated files under the main file, but this feature can be used even for your class, as shown in Figure1.

image

Figure 1 : Two code files nested inside program.cs

There are a lot of reasons to group files togheter, you can use this technique for group partial classes definition or you can simply want to group logically related files, etc etc. The annoying stuff is that there is no menu in Visual Studio that permits you to obtain this result, but you can solve everything with a  macro. Just open the Visual Studio Macro IDE and insert the following macro.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Sub GroupFileTogether()
 
Dim lvProcesses As New ListView
For I As Int32 = 1 To DTE.SelectedItems.Count
Dim item As EnvDTE.SelectedItem = DTE.SelectedItems.Item(I)
Dim lvi As New ListViewItem
lvi.Tag = item
lvi.Text = item.Name
lvProcesses.Items.Add(lvi)
Next
 
If lvProcesses.Items.Count < 2 Then
Return
End If
 
 
Dim frm As New Form
Dim btn As New Button
btn.Text = "OK"
btn.DialogResult = DialogResult.OK
frm.Controls.Add(btn)
frm.Width = 300
frm.Text = "Choose the file to be used as root"
btn.Dock = DockStyle.Bottom
frm.Controls.Add(lvProcesses)
lvProcesses.Dock = DockStyle.Fill
lvProcesses.View = View.Details
lvProcesses.Columns.Add("Name", 300, HorizontalAlignment.Left)
lvProcesses.FullRowSelect = True
 
If frm.ShowDialog() = DialogResult.OK Then
Dim selected As EnvDTE.SelectedItem = lvProcesses.SelectedItems.Item(0).Tag
 
For I As Int32 = 0 To lvProcesses.Items.Count - 1
Dim item As EnvDTE.SelectedItem = lvProcesses.Items.Item(I).Tag
If item.Name <> selected.Name Then
selected.ProjectItem.ProjectItems.AddFromFile( _
item.ProjectItem.FileNames(0))
End If
Next
End If
End Sub

This is far from being called production code ready, but it does its dirty work, just assign a shortcut to this macro from the Tools->Customize menu (I’ve used CTRL+G, CTRL+R), then you should select all the files you want to group together and press the shortcut, the macro will show you a dialog that permits you to choose which is the file to be used as root, as shown in Figure2 image

Figure 2 : The macro is showing you the list of selected files to choose the root file

Just select Program.cs and press ok and the files will be grouped together as shown in Figure1.

Gian Maria