diff --git a/src/CodingWithCalvin.OpenInNotepadPlusPlus/Helpers/ProjectHelpers.cs b/src/CodingWithCalvin.OpenInNotepadPlusPlus/Helpers/ProjectHelpers.cs index f7c10aa..d30b415 100644 --- a/src/CodingWithCalvin.OpenInNotepadPlusPlus/Helpers/ProjectHelpers.cs +++ b/src/CodingWithCalvin.OpenInNotepadPlusPlus/Helpers/ProjectHelpers.cs @@ -1,4 +1,5 @@ using System; +using System.IO; using EnvDTE; using EnvDTE80; using Microsoft.VisualStudio.Shell; @@ -7,6 +8,8 @@ namespace CodingWithCalvin.OpenInNotepadPlusPlus.Helpers { internal static class ProjectHelpers { + private const string UnloadedProjectGuid = "{67294A52-A4F0-11D2-AA88-00C04F688DDE}"; + public static string GetSelectedPath(DTE2 dte) { ThreadHelper.ThrowIfNotOnUIThread(); @@ -21,12 +24,38 @@ UIHierarchyItem selectedItem in (Array) case ProjectItem projectItem: return projectItem.FileNames[1]; case Project project: - return project.FullName; + return GetProjectPath(project, dte.Solution); case Solution solution: return solution.FullName; } } return null; } + + private static string GetProjectPath(Project project, Solution solution) + { + ThreadHelper.ThrowIfNotOnUIThread(); + + bool isUnloaded = project.Kind.Equals(UnloadedProjectGuid, StringComparison.OrdinalIgnoreCase); + + if (!isUnloaded) + { + return project.FullName; + } + + // For unloaded projects, FullName is empty but UniqueName contains + // the relative path from the solution directory + if (!string.IsNullOrEmpty(project.UniqueName) && !string.IsNullOrEmpty(solution?.FullName)) + { + var solutionDirectory = Path.GetDirectoryName(solution.FullName); + var projectPath = Path.Combine(solutionDirectory, project.UniqueName); + if (File.Exists(projectPath)) + { + return projectPath; + } + } + + return null; + } } }