2 minor modifications to make it work on VS 2008 for web site projects:
(4 new mappings):
extensionMap.Add(".master", New String() {".master.cs"})
extensionMap.Add(".cs", New String() {""})
extensionMap.Add(".aspx", New String() {".aspx.cs"})
extensionMap.Add(".ascx", New String() {".ascx.cs"})
for .cs files the mapping is empty as file names are like "blabla.aspx.cs" and all we have to do is remove ".cs" at the end
....
also when opening the file it's complete path should be specified:
If OpenFile(fileInfo.DirectoryName + "\" + baseName + ext) = True Then Exit Sub
Here is the entire code I use at this point:
'' Attempts to open the given file, and returns a boolean indicating success or failure
'' Never throws.
Function OpenFile(ByVal filePath As String)
Try
DTE.ItemOperations.OpenFile(filePath)
Return True
Catch ex As Exception
Return False
End Try
End Function
'' Opens the corresponding header/source file
'' Current file | Target file(s)
'' X.hpp | X.cpp
'' X.h | X.c, else X.cpp
'' X.cpp | X.hpp, else X.h
'' X.c | X.h
Public Sub nss_ToggleDesignAndCode_macro()
If DTE.ActiveDocument Is Nothing Then Exit Sub
Dim fileInfo As FileInfo = New FileInfo(DTE.ActiveDocument.FullName)
Dim extensionMap As New Dictionary(Of String, String())
extensionMap.Add(".hpp", New String() {".cpp"})
extensionMap.Add(".h", New String() {".c", ".cpp"})
extensionMap.Add(".cpp", New String() {".hpp", ".h"})
extensionMap.Add(".c", New String() {".h"})
extensionMap.Add(".master", New String() {".master.cs"})
extensionMap.Add(".cs", New String() {""})
extensionMap.Add(".aspx", New String() {".aspx.cs"})
extensionMap.Add(".ascx", New String() {".ascx.cs"})
Dim currentExt As String = LCase(fileInfo.Extension)
If extensionMap.ContainsKey(currentExt) = False Then Exit Sub
Dim extensions As String() = extensionMap(currentExt)
Dim baseName = Path.GetFileNameWithoutExtension(fileInfo.FullName)
For Each ext As String In extensions
If OpenFile(fileInfo.DirectoryName + "\" + baseName + ext) = True Then Exit Sub
Next
MsgBox("Could not find any corresponding file to open")
End Sub