-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileExplorer.cs
More file actions
167 lines (155 loc) · 6.02 KB
/
FileExplorer.cs
File metadata and controls
167 lines (155 loc) · 6.02 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/// copy from
/// https://github.com/heku/Kool.VsDiff/blob/main/Kool.VsDiff.Shared/Models/VS.cs
using EnvDTE;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using static FileUtil.FileUtilPackage;
namespace FileUtil
{
public static class SolutionExplorer
{
private static readonly string SystemDirectorySeparator = Path.DirectorySeparatorChar.ToString();
public static string TryGetSingleSelectedFile()
{
ThreadHelper.ThrowIfNotOnUIThread();
try
{
var selectedItems = Dte.SelectedItems;
if (selectedItems.Count < 1)
{
return "";
}
var files = GetSelectedFiles(selectedItems);
if (files?.Count == 1)
{
return files[0].File;
}
}
catch (Exception)
{
}
return "";
}
public static bool TryGetSelectedFiles(out string name1, out string name2, out string file1, out string file2)
{
ThreadHelper.ThrowIfNotOnUIThread();
name1 = name2 = file1 = file2 = null;
try
{
var selectedItems = Dte.SelectedItems;
if (selectedItems.Count != 2)
{
return false;
}
var files = GetSelectedFiles(selectedItems);
if (files?.Count == 2)
{
name1 = files[0].Name;
file1 = files[0].File;
name2 = files[1].Name;
file2 = files[1].File;
return !string.IsNullOrEmpty(file1) && !string.IsNullOrEmpty(file2);
}
}
catch (Exception ex)
{
Debug.WriteLine($"Failed to get selected files: {ex.Message}.");
}
return false;
}
private struct NameFile
{
public string Name;
public string File;
}
private static List<NameFile> GetSelectedFiles(SelectedItems selectedItems)
{
ThreadHelper.ThrowIfNotOnUIThread();
List<NameFile> files = null;
foreach (SelectedItem item in selectedItems)
{
// The index of file names from 1 to FileCount for the project item
// https://docs.microsoft.com/en-us/dotnet/api/envdte.projectitem.filenames?redirectedfrom=MSDN&view=visualstudiosdk-2017#EnvDTE_ProjectItem_FileNames_System_Int16_
var file = item.ProjectItem?.FileNames[1]; // Not works for Folder View
if (file == null || file.EndsWith(SystemDirectorySeparator))
{
continue;
}
files = files ?? new List<NameFile>();
files.Add(new NameFile { Name = item.ProjectItem.Name, File = file });
}
return files ?? GetSelectedFilesInsideFolderView();
}
private static List<NameFile> GetSelectedFilesInsideFolderView()
{
ThreadHelper.ThrowIfNotOnUIThread();
var hierarchyPtr = IntPtr.Zero;
var containerPtr = IntPtr.Zero;
try
{
if (MonitorSelection != null &&
MonitorSelection.GetCurrentSelection(out hierarchyPtr, out var itemid, out var multiSelect, out containerPtr) == VSConstants.S_OK)
{
var files = new List<NameFile>();
if (itemid != VSConstants.VSITEMID_SELECTION)
{
if (itemid != VSConstants.VSCOOKIE_NIL &&
hierarchyPtr != IntPtr.Zero &&
Marshal.GetObjectForIUnknown(hierarchyPtr) is IVsHierarchy hierarchy &&
TryGetFile(hierarchy, itemid, out var file))
{
files.Add(new NameFile { File = file });
}
}
else if (multiSelect != null)
{
if (multiSelect.GetSelectionInfo(out var numberOfSelectedItems, out _) == VSConstants.S_OK &&
numberOfSelectedItems == 2)
{
var vsItemSelections = new VSITEMSELECTION[numberOfSelectedItems];
if (multiSelect.GetSelectedItems(0, numberOfSelectedItems, vsItemSelections) == VSConstants.S_OK)
{
foreach (var selection in vsItemSelections)
{
if (TryGetFile(selection.pHier, selection.itemid, out var file))
{
files.Add(new NameFile { File = file });
}
}
}
}
}
return files;
}
return null;
}
finally
{
if (hierarchyPtr != IntPtr.Zero)
{
Marshal.Release(hierarchyPtr);
}
if (containerPtr != IntPtr.Zero)
{
Marshal.Release(containerPtr);
}
}
}
private static bool TryGetFile(IVsHierarchy hierarchy, uint itemid, out string file)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (hierarchy != null && hierarchy.GetCanonicalName(itemid, out file) == VSConstants.S_OK && file != null)
{
return true;
}
file = null;
return false;
}
}
}