如何在 Visual Studio 2005 加载项中查找并打开文件?

发布于 2024-08-31 22:00:38 字数 421 浏览 4 评论 0原文

我正在使用 Visual Studio 2005 C# 制作一个外接程序,以帮助轻松地在源文件和头文件以及所有遵循类似命名结构的脚本文件之间切换。但是,目录结构将所有文件放在不同的位置,即使它们都在同一个项目中。

我几乎已经准备好了所有部分,但我无法弄清楚如何仅根据文件名在解决方案中查找和打开文件。所以我知道我来自 c:\code\project\subproject\src\blah.cpp,我想打开 c:\code\project\subproject\inc\blah.h,但我不'不一定知道 blah.h 在哪里。我可以对不同的目录路径进行硬编码,但该实用程序不够通用,不够健壮。

该解决方案有多个项目,这似乎也有点痛苦。我现在想,我必须迭代每个项目,并迭代每个项目项,以查看特定文件是否存在,然后获得对它的正确引用。

但在我看来,必须有一种更简单的方法来做到这一点。

I'm making an add-in with Visual Studio 2005 C# to help easily toggle between source and header files, as well as script files that all follow a similar naming structure. However, the directory structure has all of the files in different places, even though they are all in the same project.

I've got almost all the pieces in place, but I can't figure out how to find and open a file in the solution based only on the file name alone. So I know I'm coming from, say, c:\code\project\subproject\src\blah.cpp, and I want to open c:\code\project\subproject\inc\blah.h, but I don't necessarily know where blah.h is. I could hardcode different directory paths but then the utility isn't generic enough to be robust.

The solution has multiple projects, which seems to be a bit of a pain as well. I'm thinking at this point that I'll have to iterate through every project, and iterate through every project item, to see if the particular file is there, and then get a proper reference to it.

But it seems to me there must be an easier way of doing this.

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

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

发布评论

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

评论(1

薔薇婲 2024-09-07 22:00:38

要对任何用户的文件结构通用,您需要枚举所有项目中的所有文件。这应该可以帮助您开始。而且,好吧,差不多完成了:-)

    internal static string GetSourceOrInclude(bool openAndActivate)
    {
        // Look in the project for a file of the same name with the opposing extension
        ProjectItem thisItem = Commands.Application.ActiveDocument.ProjectItem;
        string ext = Path.GetExtension(thisItem.Name);
        string searchExt = string.Empty;
        if (ext == ".cpp" || ext == ".c")
            searchExt = ".h";
        else if (ext == ".h" || ext == ".hpp")
            searchExt = ".cpp";
        else
            return(string.Empty);

        string searchItemName = thisItem.Name;
        searchItemName = Path.ChangeExtension(searchItemName, searchExt);

        Project proj = thisItem.ContainingProject;
        foreach(ProjectItem item in proj.ProjectItems)
        {
            ProjectItem foundItem = FindChildProjectItem(item, searchItemName);
            if (foundItem != null)
            {
                if (openAndActivate)
                {
                    if (!foundItem.get_IsOpen(Constants.vsViewKindCode))
                    {
                        Window w = foundItem.Open(Constants.vsViewKindCode);
                        w.Visible = true;
                        w.Activate();
                    }
                    else
                    {
                        foundItem.Document.Activate();
                    }
                }

                return(foundItem.Document.FullName);
            }

        return(string.Empty);
    }

请注意,标头可能位于包含路径中而不添加到项目中,因此如果上述操作失败,您也可能会在包含项目的包含路径中查找。我将把它作为练习留给读者。

To work generically for any user's file structure, you'll need to enumerate all the files in all the projects. This should get you started. And, well, pretty much finished :-)

    internal static string GetSourceOrInclude(bool openAndActivate)
    {
        // Look in the project for a file of the same name with the opposing extension
        ProjectItem thisItem = Commands.Application.ActiveDocument.ProjectItem;
        string ext = Path.GetExtension(thisItem.Name);
        string searchExt = string.Empty;
        if (ext == ".cpp" || ext == ".c")
            searchExt = ".h";
        else if (ext == ".h" || ext == ".hpp")
            searchExt = ".cpp";
        else
            return(string.Empty);

        string searchItemName = thisItem.Name;
        searchItemName = Path.ChangeExtension(searchItemName, searchExt);

        Project proj = thisItem.ContainingProject;
        foreach(ProjectItem item in proj.ProjectItems)
        {
            ProjectItem foundItem = FindChildProjectItem(item, searchItemName);
            if (foundItem != null)
            {
                if (openAndActivate)
                {
                    if (!foundItem.get_IsOpen(Constants.vsViewKindCode))
                    {
                        Window w = foundItem.Open(Constants.vsViewKindCode);
                        w.Visible = true;
                        w.Activate();
                    }
                    else
                    {
                        foundItem.Document.Activate();
                    }
                }

                return(foundItem.Document.FullName);
            }

        return(string.Empty);
    }

Note that it is possible for a header to be in the include path without being added to the project, so if the above fails, you could potentially look in the include paths for the containing project too. I'll leave that as an exercise for the reader.

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