다니엘 -
편집기에서 프로젝트로 가져 오는 과정은 여러 단계로 진행됩니다. 먼저 편집기에서 파일의 파일 이름을 가져오고 포함 된 프로젝트를 찾을 수 있습니다.
이 같은 파일 이름을 얻을, 당신은 IWPFTextView을 가지고 있습니다 가정 :
public static string GetFilePath(Microsoft.VisualStudio.Text.Editor.IWpfTextView wpfTextView)
{
Microsoft.VisualStudio.Text.ITextDocument document;
if ((wpfTextView == null) ||
(!wpfTextView.TextDataModel.DocumentBuffer.Properties.TryGetProperty(typeof(Microsoft.VisualStudio.Text.ITextDocument), out document)))
return String.Empty;
// If we have no document, just ignore it.
if ((document == null) || (document.TextBuffer == null))
return String.Empty;
return document.FilePath;
}
당신은 파일 이름을 가지고하면, 당신이이 같은 부모 프로젝트의 얻을 수 있습니다 :
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Interop;
public static Project GetContainingProject(string fileName)
{
if (!String.IsNullOrEmpty(fileName))
{
var dte2 = (DTE2)Package.GetGlobalService(typeof(SDTE));
if (dte2 != null)
{
var prjItem = dte2.Solution.FindProjectItem(fileName);
if (prjItem != null)
return prjItem.ContainingProject;
}
}
return null;
}
프로젝트에서 당신은 codemodel을 얻을 수 있고, 나는 참고 문헌을 추측하지만, 아직 그렇게 할 필요가 없다. 이 도움이
희망 ...
~ 카메론
감사합니다! 참조를 얻으려면 VSProject 객체를 가져와야합니다. Project.Object를 VSProject로 형변환 할 수 있습니다. if (project.Object가 VSProject 임) {var vsProject = (VSProject) project.Object;/* ... * /} –
다른 사람들이 다음에 대한 참조 요구 사항을 만족시키지 못하는 경우 : Visual Studio 2015에서 파일 이름 조회 작업을 수행하기 위해 Microsoft.VisualStudio.Text.Logic에 대한 참조를 추가해야했습니다 'TextDataModel.DocumentBuffer'). 프로젝트 조회 작업 ('DTE2'와'Project')을 만들기 위해'EnvDTE'와'EnvDTE80'에 대한 참조를 추가해야했습니다. – Spiralis