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

标签 c# visual-studio contextmenu vsix

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

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

场景:

  1. 开放VS(2017)
  2. 导航至"file"->“打开”->"file"。
  3. 右键单击文件,然后单击所需的上下文菜单。 (它调用 IsSingleProjectItemSelection 方法)。
  4. monitorSelection.GetCurrentSelection(out HierarchyPtr 失败,因为 hierarchyPtr 仍为 IntPtr.Zero。

Value cannot be null. Parameter name: 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 返回您右键单击的文件的完整路径。

关于c# - Vsix(Visual Studio 扩展)右键单击文件的完整路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47855889/

相关文章:

delphi - 如何以编程方式选择另一个应用程序中的上下文菜单项?

android - 获取android中动态 ListView 的行ID以添加到购物车

c# - 读取字节数组

c# - json反序列化没有属性名称的数组

c# - Asp.net Dropdownlist 选中项值

c# - Visual Studio 不断崩溃

javascript - 右键单击文本框创建菜单和操作

c# - 为什么 HttpClient 在随后使用 Polly 重试期间继续失败?

asp.net - 如何在 IIS 7.5 上调试经典 asp 页面 Visual Studio 2010?

c# - 为什么 VB 中的代码片段比 C# 中的代码片段更有特色?