11using System ;
2+ using System . IO ;
23using EnvDTE ;
34using EnvDTE80 ;
45using Microsoft . VisualStudio . Shell ;
@@ -7,6 +8,8 @@ namespace CodingWithCalvin.OpenInNotepadPlusPlus.Helpers
78{
89 internal static class ProjectHelpers
910 {
11+ private const string UnloadedProjectGuid = "{67294A52-A4F0-11D2-AA88-00C04F688DDE}" ;
12+
1013 public static string GetSelectedPath ( DTE2 dte )
1114 {
1215 ThreadHelper . ThrowIfNotOnUIThread ( ) ;
@@ -21,12 +24,38 @@ UIHierarchyItem selectedItem in (Array)
2124 case ProjectItem projectItem :
2225 return projectItem . FileNames [ 1 ] ;
2326 case Project project :
24- return project . FullName ;
27+ return GetProjectPath ( project , dte . Solution ) ;
2528 case Solution solution :
2629 return solution . FullName ;
2730 }
2831 }
2932 return null ;
3033 }
34+
35+ private static string GetProjectPath ( Project project , Solution solution )
36+ {
37+ ThreadHelper . ThrowIfNotOnUIThread ( ) ;
38+
39+ bool isUnloaded = project . Kind . Equals ( UnloadedProjectGuid , StringComparison . OrdinalIgnoreCase ) ;
40+
41+ if ( ! isUnloaded )
42+ {
43+ return project . FullName ;
44+ }
45+
46+ // For unloaded projects, FullName is empty but UniqueName contains
47+ // the relative path from the solution directory
48+ if ( ! string . IsNullOrEmpty ( project . UniqueName ) && ! string . IsNullOrEmpty ( solution ? . FullName ) )
49+ {
50+ var solutionDirectory = Path . GetDirectoryName ( solution . FullName ) ;
51+ var projectPath = Path . Combine ( solutionDirectory , project . UniqueName ) ;
52+ if ( File . Exists ( projectPath ) )
53+ {
54+ return projectPath ;
55+ }
56+ }
57+
58+ return null ;
59+ }
3160 }
3261}
0 commit comments