2017-12-17 32 views
1

(Visual Studio 2017のエディタ内で)右クリックしたファイルのフルパスを取得したいと考えています。プロジェクトやソリューションが開かれたときにファイルのパスを取得するために、次のコードを実装しました。右クリックしたファイルのフルパスVsix(Visual Studio extension)

この実装は、1つのファイルを開くと機能しません。

シナリオ:

  1. オープンVS(2017)
  2. 移動ファイルへ - >開く - >ファイル。
  3. ファイルを右クリックして、目的のコンテキストメニューをクリックします。 (IsSingleProjectItemSelectionメソッドを呼び出します)。
  4. 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); 
     } 
    } 
} 

答えて

2

DTE.ActiveDocument.FullName戻りフルパス。