-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProjectHelpers.cs
More file actions
61 lines (53 loc) · 1.94 KB
/
ProjectHelpers.cs
File metadata and controls
61 lines (53 loc) · 1.94 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
using System;
using System.IO;
using EnvDTE;
using EnvDTE80;
using Microsoft.VisualStudio.Shell;
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();
foreach (
UIHierarchyItem selectedItem in (Array)
dte.ToolWindows.SolutionExplorer.SelectedItems
)
{
switch (selectedItem.Object)
{
case ProjectItem projectItem:
return projectItem.FileNames[1];
case Project project:
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;
}
}
}