C# Visual Studio扩展:如何获取;路径“;选择时解决方案文件夹的名称?
上一个问题: 我构建了一个能够正确管理Git模块的扩展,为此,我在解决方案中有一个名为SubModules的文件夹 我设法将上下文菜单添加到解决方案文件和项目文件中,但我想在右键单击我的子模块文件夹(即删除、添加)时禁用一些本机命令 我现在知道如何禁用所需的本机命令,但要做到这一点,我需要获取所选解决方案文件夹的“路径” 我尝试实现IVsSelectionEvents,但运气不佳(在施放之前o不为null): 使用我的代码(来自另一个问题)(我想找到“路径”,但我可以用上面的“解决方案”进行管理),我尝试转换到SolutionFolder或FileProperties。。。仍然没有运气;即使MSDN告诉我们返回的对象应该是FileProperties类型。使用QuickWatch在C# Visual Studio扩展:如何获取;路径“;选择时解决方案文件夹的名称?,c#,visual-studio,visual-studio-2017,vsix,C#,Visual Studio,Visual Studio 2017,Vsix,上一个问题: 我构建了一个能够正确管理Git模块的扩展,为此,我在解决方案中有一个名为SubModules的文件夹 我设法将上下文菜单添加到解决方案文件和项目文件中,但我想在右键单击我的子模块文件夹(即删除、添加)时禁用一些本机命令 我现在知道如何禁用所需的本机命令,但要做到这一点,我需要获取所选解决方案文件夹的“路径” 我尝试实现IVsSelectionEvents,但运气不佳(在施放之前o不为null): 使用我的代码(来自另一个问题)(我想找到“路径”,但我可以用上面的“解决方案”进行管
(Marshal.GetObjectForIUnknown(selectionContainerPtr)作为iSelectContainer)上进行探索,我可以进入查看私有属性\u节点
,在那里,我可以查看我的SolutionFolder节点,并使用其父属性来确保它是我要阻止命令的文件夹。以下是实际代码:
private static void CommandEvents_BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
string name = GetCommandName(Guid, ID);
if (name == "Project.AddNewSolutionFolder")
{
CancelDefault = true;
}
if (name == "Edit.Delete")
{
CancelDefault = true;
IVsMultiItemSelect multiItemSelect = null;
IntPtr hierarchyPtr = IntPtr.Zero;
IntPtr selectionContainerPtr = IntPtr.Zero;
uint itemid = 0;
int hr = monitorSelection.GetCurrentSelection(out hierarchyPtr, out itemid, out multiItemSelect, out selectionContainerPtr);
object[] selected = new object[2];
(Marshal.GetObjectForIUnknown(selectionContainerPtr) as ISelectionContainer).GetObjects(2, 1, selected);
var t = selected[0] as VSLangProj.FileProperties; // ==> null
var t2 = selected[0] as SolutionFolder; // ==> null
var b = 1;
}
if (name == "View.Branch")
{
//TODO: Could disable this command if not able to find the branch changing command (that would be cancelled and launch ours)
menuBranchOpened = true;
}
if (menuBranchOpened)
{
var a = 1;
}
}
我受到了启发并尝试了(除了上面的一个似乎真的很接近外,没有一个有效):
最后,在浏览(更多)回答我上一个问题的另一个人的网站时,我终于找到了它。因此,我将分享一个我制作的方法,以了解在解决方案资源管理器中选择的项目类型
applicationObject = await ServiceProvider.GetGlobalServiceAsync(typeof(SDTE)) as DTE2; // Was initiate in a method
private enum SelectionTypes
{
Other = 0,
InSubModules = 1,
IsAFolder = 2,
IsAProject = 4,
InAProject = 8
}
private static List<SelectionTypes> GetSelectionTypes()
{
ThreadHelper.ThrowIfNotOnUIThread();
var selectionTypes = new List<SelectionTypes>();
EnvDTE.UIHierarchy solutionExplorer = applicationObject.ToolWindows.SolutionExplorer;
object[] items = solutionExplorer.SelectedItems as object[];
//{ Name = "WindowBase" FullName = "Microsoft.VisualStudio.Platform.WindowManagement.DTE.WindowBase"}
if (items.Length > 0)
{
for (int i = 0; i < items.Length; i++)
{
var selectionType = SelectionTypes.Other;
var selectedItem = items[0] as EnvDTE.UIHierarchyItem;
var currentItem = selectedItem;
var subModulesParentsCount = 0;
var countingSubModulesParents = false;
var nbParents = -1;
if (currentItem.Object.GetType().FullName == "Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject") selectionType |= SelectionTypes.IsAProject;
if (currentItem.Object.GetType().FullName == "Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAFolderItem") selectionType |= SelectionTypes.IsAFolder;
while (currentItem != null)
{
nbParents++;
if (countingSubModulesParents) subModulesParentsCount++;
if (currentItem.Name == "SubModules")
{
subModulesParentsCount = 0;
countingSubModulesParents = true;
}
if (currentItem.Object.GetType().FullName == "Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject") selectionType |= SelectionTypes.InAProject;
currentItem = currentItem.Collection.Parent as EnvDTE.UIHierarchyItem;
}
if (selectionType == SelectionTypes.Other && nbParents != 0) selectionType |= SelectionTypes.IsAFolder;
if (subModulesParentsCount == 1)
{
selectionType |= SelectionTypes.InSubModules;
}
selectionTypes.Add(selectionType);
}
}
return selectionTypes;
}
applicationObject=wait ServiceProvider.GetGlobalServiceAsync(typeof(SDTE))作为DTE2;//是在一种方法中启动的
私有枚举选择类型
{
其他=0,
InSubModules=1,
IsAFolder=2,
IsAProject=4,
INA项目=8
}
私有静态列表GetSelectionTypes()
{
ThreadHelper.throwifnotunuithread();
var selectionTypes=新列表();
EnvDTE.ui层次结构solutionExplorer=applicationObject.ToolWindows.solutionExplorer;
object[]items=solutionExplorer.SelectedItems作为object[];
//{Name=“WindowBase”FullName=“Microsoft.VisualStudio.Platform.WindowManagement.DTE.WindowBase”}
如果(items.Length>0)
{
对于(int i=0;i
最后,我浏览了(更多)回答我上一个问题的另一个人的网站,终于找到了它。因此,我将分享一个我制作的方法,以了解在解决方案资源管理器中选择的项目类型
applicationObject = await ServiceProvider.GetGlobalServiceAsync(typeof(SDTE)) as DTE2; // Was initiate in a method
private enum SelectionTypes
{
Other = 0,
InSubModules = 1,
IsAFolder = 2,
IsAProject = 4,
InAProject = 8
}
private static List<SelectionTypes> GetSelectionTypes()
{
ThreadHelper.ThrowIfNotOnUIThread();
var selectionTypes = new List<SelectionTypes>();
EnvDTE.UIHierarchy solutionExplorer = applicationObject.ToolWindows.SolutionExplorer;
object[] items = solutionExplorer.SelectedItems as object[];
//{ Name = "WindowBase" FullName = "Microsoft.VisualStudio.Platform.WindowManagement.DTE.WindowBase"}
if (items.Length > 0)
{
for (int i = 0; i < items.Length; i++)
{
var selectionType = SelectionTypes.Other;
var selectedItem = items[0] as EnvDTE.UIHierarchyItem;
var currentItem = selectedItem;
var subModulesParentsCount = 0;
var countingSubModulesParents = false;
var nbParents = -1;
if (currentItem.Object.GetType().FullName == "Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject") selectionType |= SelectionTypes.IsAProject;
if (currentItem.Object.GetType().FullName == "Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAFolderItem") selectionType |= SelectionTypes.IsAFolder;
while (currentItem != null)
{
nbParents++;
if (countingSubModulesParents) subModulesParentsCount++;
if (currentItem.Name == "SubModules")
{
subModulesParentsCount = 0;
countingSubModulesParents = true;
}
if (currentItem.Object.GetType().FullName == "Microsoft.VisualStudio.ProjectSystem.VS.Implementation.Package.Automation.OAProject") selectionType |= SelectionTypes.InAProject;
currentItem = currentItem.Collection.Parent as EnvDTE.UIHierarchyItem;
}
if (selectionType == SelectionTypes.Other && nbParents != 0) selectionType |= SelectionTypes.IsAFolder;
if (subModulesParentsCount == 1)
{
selectionType |= SelectionTypes.InSubModules;
}
selectionTypes.Add(selectionType);
}
}
return selectionTypes;
}
applicationObject=wait ServiceProvider.GetGlobalServiceAsync(typeof(SDTE))作为DTE2;//是在一种方法中启动的
私有枚举选择类型
{
其他=0,
InSubModules=1,
IsAFolder=2,
IsAProject=4,
INA项目=8
}
私有静态列表GetSelectionTypes()
{
ThreadHelper.throwifnotunuithread();
var selectionTypes=新列表();
EnvDTE.ui层次结构solutionExplorer=applicationObject.ToolWindows.solutionExplorer;
object[]items=solutionExplorer.SelectedItems作为object[];
//{Name=“WindowBase”FullName=“Microsoft.VisualStudio.Platform.WindowManagement.DTE.WindowBase”}
如果(items.Length>0)
{
对于(int i=0;i