我正在VSIX项目上工作,我想在其中获得VS IDE中正在编辑的当前文件名和路径?我该怎么办?
首先,在TextAdornment1TextViewCreationListener.cs中添加ITextDocumentFactoryService
并与TextAdornment1
一起传递
[Import]
public ITextDocumentFactoryService textDocumentFactoryService { get; set; }
public void TextViewCreated(IWpfTextView textView)
{
new TextAdornment1(textView, this.textDocumentFactoryService);
}
现在,在TextAdornment1中,全局添加ITextDocumentFactoryService
和ITextDocument
。请遵循以下代码以获取详细信息。
private readonly ITextDocumentFactoryService _textDocumentFactoryService;
private ITextDocument TextDocument;
public TextAdornment1(IWpfTextView view, ITextDocumentFactoryService textDocumentFactoryService)
{
this._textDocumentFactoryService = textDocumentFactoryService;
}
internal async void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
{
var currentDocInfo = this._textDocumentFactoryService.TryGetTextDocument(this.view.TextBuffer, out this.TextDocument);
if(currentDocInfo)
{
MessageBox.Show(this.TextDocument.FilePath.);
}
else
{
MessageBox.Show("Nothingg!");
}
}
答案参考:Here