Danielle -
Getting from the project editor is a multi-step process. First you get the file name in the editor, and from there you can find the contained project.
Assuming you have an IWPFTextView, you can get the file name as follows:
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; }
Once you have the file name, you can get its parent project as follows:
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; }
From the project, you can go to codemodel, and I assume the links, but I don't need to do this yet.
Hope this helps ...
~ Cameron
source share