Skip to content

Commit 71678cf

Browse files
committed
Fix file path resolution for unloaded projects
When a project is unloaded in Solution Explorer, project.FullName returns an empty string. This change detects unloaded projects by checking the project.Kind against the unloaded project GUID, then falls back to constructing the path from the solution directory and project.UniqueName.
1 parent 4c19dda commit 71678cf

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/CodingWithCalvin.OpenInNotepadPlusPlus/Helpers/ProjectHelpers.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using EnvDTE;
34
using EnvDTE80;
45
using 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

Comments
 (0)