简体   繁体   English

扩展Visual Studio根据文件路径获取ProjectItem或IVsHierarchy

[英]Extending Visual Studio getting ProjectItem or IVsHierarchy based on file path

I'm trying to build an extension for Visual Studio 2017, the idea is to automatically include files that are created in a certain folder on disk (using a FileSystemWatcher), any changes to folder names and/or filenames should also be reflected (included) i the Project. 我正在尝试为Visual Studio 2017建立扩展,其想法是自动包括在磁盘上某个文件夹中创建的文件(使用FileSystemWatcher),对文件夹名称和/或文件名的任何更改也应反映(包括)我的项目。

I've looked around for an existing extension without any luck. 我四处寻找没有任何运气的现有扩展。

After working on this for a while I'm quite close to a solution but the current approach that I have for removing items from the Solution Explorer when file/foldernames are changed/deleted does not scale when the solution is bigger (have many files in it). 经过一段时间的研究之后,我已经很接近解决方案了,但是当解决方案较大时,当文件/文件夹名称被更改/删除时,从解决方案资源管理器中删除项目的当前方法无法扩展(解决方案较大时,它)。 My current approach is to use the root IVsHierarchy and process it recursively and then find a file/folder based on the "canonicalName". 我当前的方法是使用根IVsHierarchy并对其进行递归处理,然后基于“ canonicalName”找到文件/文件夹。 This means that I have to scan the solution each time i need to find a folder. 这意味着我每次需要查找文件夹时都必须扫描解决方案。 The solution is inspired by this code https://www.mztools.com/articles/2014/MZ2014007.aspx 该解决方案受此代码https://www.mztools.com/articles/2014/MZ2014007.aspx的启发

I started out with using Project.ProjectItems but this collection would sometimes just contain a "root" folder ie with these files on disk 我从使用Project.ProjectItems开始,但是此集合有时仅包含一个“根”文件夹,即这些文件在磁盘上

/Test/MyFile.txt
/Test/Folder1
/Test/Folder2
/Test/Folder3

The only ProjectItem that I could find was /Test/, this does not work if I need to remove /Test/Folder1 ie. 我可以找到的唯一ProjectItem是/ Test /,如果我需要删除/ Test / Folder1即无法使用。 The approach with IVsHierarchy gave me all the folder and the implementation works but are slow. 使用IVsHierarchy的方法给了我所有的文件夹和实现工作,但是很慢。

So, my question: 所以,我的问题是:

Is there a way to get a reference to a ProjectItem based on the full path of the file/folder on disk? 有没有一种方法可以基于磁盘上文件/文件夹的完整路径获取对ProjectItem的引用? So that I don't have to iterate over all items in the solution to find my ProjectItem and slow down everything. 这样我就不必遍历解决方案中的所有项目来找到我的ProjectItem并放慢一切。 It would also work with a reference to a IVsHierarchy that I can use to get the ProjectItem. 它也可以与对IVsHierarchy的引用一起使用,我可以用来获取ProjectItem。

Any help would be very much appreciated. 任何帮助将不胜感激。

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM