在C#VSIX中,如何获取当前文件名及其当前正在编辑的路径?

问题描述 投票:0回答:1

我正在VSIX项目上工作,我想在其中获得VS IDE中正在编辑的当前文件名和路径?我该怎么办?

c# visual-studio-extensions vsix vs-extensibility extensibility
1个回答
0
投票

首先,在TextAdornment1TextViewCreationListener.cs中添加ITextDocumentFactoryService并与TextAdornment1一起传递

[Import]
public ITextDocumentFactoryService textDocumentFactoryService { get; set; }

public void TextViewCreated(IWpfTextView textView)
{
    new TextAdornment1(textView, this.textDocumentFactoryService);
}

现在,在TextAdornment1中,全局添加ITextDocumentFactoryServiceITextDocument。请遵循以下代码以获取详细信息。

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

© www.soinside.com 2019 - 2024. All rights reserved.