[英]How to find the file name of a the document that belongs to an ITextView?
我目前正在編寫一個 Visual Studio 擴展,並在一個實現ITextViewCreationListener
接口的類中實現了函數TextViewCreated
。
該函數使用ITextView
參數調用,該參數表示剛剛創建的編輯器窗口的內容。 但是,我不僅需要編輯文檔的內容,還需要它所代表的文件的路徑,而ITextView
對象顯然沒有獲取該信息的方法。
到目前為止,我已經使用了DTE2.ActiveDocument
屬性,但它並不總是能正常工作。 特別是如果一個人在 Visual Studio 中打開一個新的文本窗口,而另一個已經打開, ActiveDocument
指的是前一個文檔。
可以做些什么來糾正這一點?
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.