Vsix(Visual Studio扩展)右键单击文件的完整路径 [英] Vsix (Visual Studio extension) full path of right-clicked file

查看:108
本文介绍了Vsix(Visual Studio扩展)右键单击文件的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to retrieve the full path of a right-clicked file (within the editor of Visual Studio 2017). I already implemented the following code to retrieve the path of a file when a project and/or solution is opened.

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

场景:

  1. Open VS(2017)
  2. 导航到文件->打开->文件.
  3. 右键单击文件,然后单击所需的上下文菜单.(它调用IsSingleProjectItemSelection方法).
  4. monitorSelection.GetCurrentSelection(out boundaryPtr 失败,因为 hierarchyPtr 仍为IntPtr.Zero.
  1. Open VS (2017)
  2. Navigate to File -> Open -> File.
  3. Right click on a file and click on the desired context menu. (It calls the IsSingleProjectItemSelection method).
  4. monitorSelection.GetCurrentSelection(out hierarchyPtr fails, because hierarchyPtr remains IntPtr.Zero.

值不能为null.参数名称:pUnk

Value cannot be null. Parameter name: pUnk

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

Perhaps you know a solution to retrieve the full path of a right-clicked file within the editor of 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 返回您右键单击的文件的完整路径.

DTE.ActiveDocument.FullName returns full path of the file you right clicked in.

这篇关于Vsix(Visual Studio扩展)右键单击文件的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆