Visual Studio 加载项:无法从 SelectedItems 获取 Document 对象

发布于 2024-09-14 16:56:18 字数 601 浏览 0 评论 0原文

首先我要说的是,我是 Visual Studio 开发插件的新手...

当使用 DTE2 对象时,我试图获取有关当前所选文档的一些信息。如果我这样做:

DTE2 VisualStudioInstance = somethingOrOther;
Document documentInfo;

if(VisualStudioInstance.SelectedItems.MultiSelect == false)
{
    documentInfo = VisualStudioInstance.SelectedItems.Item(1).ProjectItem.Document;
}

Document 对象似乎总是为空。 VisualStudioInstance.SelectedItems.Item(1).Project 也是 null (我不一定需要,但这似乎也很奇怪。它确实给了我所选项目的名称,所以我至少知道它正在寻找一些东西 现在,

如果我直接转到 VisualStudioInstance.ActiveDocument,我会获得所需的所有信息。是否确实必须打开文档才能获取信息?如果是这样,我还可以如何获取所选项目的文档信息?我要查找的主要内容是所选文件的完整路径信息。

Let me start by saying I'm new to developing add-ins for visual studio...

When using the DTE2 object, I am attempting to get some info on the document that is currently selected. If I do something like this:

DTE2 VisualStudioInstance = somethingOrOther;
Document documentInfo;

if(VisualStudioInstance.SelectedItems.MultiSelect == false)
{
    documentInfo = VisualStudioInstance.SelectedItems.Item(1).ProjectItem.Document;
}

The Document object always seems to be null. VisualStudioInstance.SelectedItems.Item(1).Project is also null (which I don't necessarily need, but that seems odd as well. It does give me the name of the selected item, so I know at least that it is finding something.

Now if I go directly to VisualStudioInstance.ActiveDocument, I get all the info I am looking for. Does the document actually have to be open to get the information? If so, how else would I go about getting document info for a selected item without opening it? The main thing I'm looking for is full path information for the selected file. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

手心的温暖 2024-09-21 16:56:18

实际上你可以使用 ProjectItem.FileNames 属性。它是一个索引属性,即使文档关闭也有效。

Actually you can use ProjectItem.FileNames property. It is an indexed property which is valid even if the document is closed.

始于初秋 2024-09-21 16:56:18

如果它已关闭,请先打开它,然后文档实例应该可用。

if (!projectItem.IsOpen)
{
    projectItem.Open();
}

Open it first if it is closed, then the document instance should be available.

if (!projectItem.IsOpen)
{
    projectItem.Open();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文