1
(Visual Studio 2017의 편집기 내에서) 오른쪽 클릭 한 파일의 전체 경로를 검색하고 싶습니다. 이미 프로젝트 및/또는 솔루션을 열 때 파일 경로를 검색하기 위해 다음 코드를 구현했습니다.오른쪽 클릭 한 파일의 전체 경로 Vsix (Visual Studio 확장)
단일 파일을 열면이 구현이 작동하지 않습니다.
시나리오 :
- 열기 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);
}
}
}