I am currently writing a Visual Studio extension and have implemented the function TextViewCreated
in a class that implements an ITextViewCreationListener
interface.
The function is called with an ITextView
parameter that represent the content of the editor window that just has been created. However, I need not just the contend of the edited document but also the path the file it represents, and an ITextView
object apparently has no method to get that information.
So far, I have used the DTE2.ActiveDocument
property, but it does not always work correctly. Especially if one opens a new text window in Visual Studio while another one is already open, ActiveDocument
refers to the previous document.
What can one do to correct this?
using Microsoft.VisualStudio.Text;
public string GetDocumentPath(ITextView view)
{
if (view.TextBuffer.Properties.TryGetProperty(typeof(ITextDocument), out ITextDocument textDocument) && textDocument is not null)
{
return textDocument.FilePath;
}
return null;
}