Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Vsix(Visual Studio扩展名)右键单击文件的完整路径_C#_Visual Studio_Contextmenu_Vsix - Fatal编程技术网

C# Vsix(Visual Studio扩展名)右键单击文件的完整路径

C# Vsix(Visual Studio扩展名)右键单击文件的完整路径,c#,visual-studio,contextmenu,vsix,C#,Visual Studio,Contextmenu,Vsix,我想检索右键单击文件的完整路径(在Visual Studio 2017的编辑器中)。我已经实现了以下代码,用于在打开项目和/或解决方案时检索文件的路径 如果打开单个文件,此实现将不起作用 场景: 开放VS(2017年) 导航到文件->打开->文件 在文件上单击鼠标右键,然后单击所需的关联菜单。(它调用IsSingleProjectItemSelection方法) monitorSelection.GetCurrentSelection(out hierarchyPtr失败,因为hierarchy

我想检索右键单击文件的完整路径(在Visual Studio 2017的编辑器中)。我已经实现了以下代码,用于在打开项目和/或解决方案时检索文件的路径

如果打开单个文件,此实现将不起作用

场景:

  • 开放VS(2017年)
  • 导航到文件->打开->文件
  • 在文件上单击鼠标右键,然后单击所需的关联菜单。(它调用IsSingleProjectItemSelection方法)
  • monitorSelection.GetCurrentSelection(out hierarchyPtr
    失败,因为
    hierarchyPtr
    保持IntPtr.Zero
  • 值不能为null。参数名称:pUnk

    也许您知道在Visual Studio编辑器(2017)中检索右键单击文件的完整路径的解决方案

    先谢谢你

        if (!IsSingleProjectItemSelection(out hierarchy, out itemid)) return;
            // Get the file path
            string itemFullPath = null;
            ((IVsProject) hierarchy).GetMkDocument(itemid, out itemFullPath);
            var transformFileInfo = new FileInfo(itemFullPath);    
            string fullPath = itemFullPath.FullName;
    
    public static bool IsSingleProjectItemSelection(out IVsHierarchy   hierarchy, out uint itemid)
    {
        hierarchy = null;
        itemid = VSConstants.VSITEMID_NIL;
        int hr = VSConstants.S_OK;
    
        var monitorSelection = Package.GetGlobalService(typeof(SVsShellMonitorSelection)) as IVsMonitorSelection;
        var solution = Package.GetGlobalService(typeof(SVsSolution)) as IVsSolution;
        if (monitorSelection == null || solution == null)
        {
            return false;
        }
    
        IVsMultiItemSelect multiItemSelect = null;
        IntPtr hierarchyPtr = IntPtr.Zero;
        IntPtr selectionContainerPtr = IntPtr.Zero;
    
        try
        {
            hr = monitorSelection.GetCurrentSelection(out hierarchyPtr, out itemid, out multiItemSelect, out selectionContainerPtr);
    
            if (ErrorHandler.Failed(hr) || hierarchyPtr == IntPtr.Zero || itemid == VSConstants.VSITEMID_NIL)
            {
                // there is no selection
                return false;
            }
    
            // multiple items are selected
            if (multiItemSelect != null) return false;
    
            // there is a hierarchy root node selected, thus it is not a single item inside a project
    
            if (itemid == VSConstants.VSITEMID_ROOT) return false;
    
            hierarchy = Marshal.GetObjectForIUnknown(hierarchyPtr) as IVsHierarchy;
            if (hierarchy == null) return false;
    
            Guid guidProjectID = Guid.Empty;
    
            if (ErrorHandler.Failed(solution.GetGuidOfProject(hierarchy, out guidProjectID)))
            {
                return false; // hierarchy is not a project inside the Solution if it does not have a ProjectID Guid
            }
    
            // if we got this far then there is a single project item selected
            return true;
        }
        finally
        {
            if (selectionContainerPtr != IntPtr.Zero)
            {
                Marshal.Release(selectionContainerPtr);
            }
    
            if (hierarchyPtr != IntPtr.Zero)
            {
                Marshal.Release(hierarchyPtr);
            }
        }
    }
    

    DTE.ActiveDocument.FullName返回右键单击的文件的完整路径