Visual Studio 扩展性:将现有文件夹添加到项目中
我正在尝试使用 Visual Studio 2008 的可扩展性来编写一个外接程序,该外接程序将在解析接口后创建一个包含各种消息的项目文件夹。 但是,我在创建/添加文件夹的步骤中遇到了麻烦。 我尝试使用
ProjectItem folder =
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty);
(项目是我的目标文件,我在旁边创建一个同名的文件夹,但附加了“消息”),但当文件夹已经存在时,它会窒息(没什么大惊喜)。
如果它已经存在,我尝试删除它,例如:
DirectoryInfo dirInfo = new DirectoryInfo(newDirectoryParent +
newDirectoryName);
if (dirInfo.Exists)
{
dirInfo.Delete(true);
}
ProjectItem folder =
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty);
我可以看到该文件夹在调试时被删除,但它仍然 似乎认为该文件夹仍然存在并且已经死在一个文件夹上 存在异常。
有任何想法吗???
谢谢。
AK
....也许答案在于删除后以编程方式刷新项目? 这怎么办呢?
I'm trying to use Visual Studio 2008's extensibility to write an addin that will create a project folder with various messages in it after parsing an interface. I'm having trouble at the step of creating/adding the folder, however. I've tried using
ProjectItem folder =
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty);
(item is my target file next to which I'm creating a folder with the same name but "Messages" appended to it) but it chokes when a folder already exists (no big surprise).
I tried deleting it if it already exists, such as:
DirectoryInfo dirInfo = new DirectoryInfo(newDirectoryParent +
newDirectoryName);
if (dirInfo.Exists)
{
dirInfo.Delete(true);
}
ProjectItem folder =
item.ProjectItem.Collection.AddFolder(newDirectoryName, string.Empty);
I can SEE that the folder gets deleted when in debug, but it still
seems to think the folder is still there and dies on a folder already
exists exception.
Any ideas???
Thanks.
AK
.... Perhaps the answer would lie in programmatically refreshing the project after the delete? How might this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
ProjectItems.AddFromDirectory会将目录及其下的所有内容添加到项目中。
ProjectItems.AddFromDirectory will add the directory and everything underneath the directory to the project.
是的,就是这样...
如果有一种更优雅的方式来做到这一点,我们将不胜感激...
谢谢。
Yup, that was it...
If there's a more elegant way of doing this, it would be much appreciated...
Thanks.
这是我的方法:
This is my approach:
我正在开发 Visual Studio 2019 的扩展,并且遇到了类似的问题。 下一页中提出的问题帮助了我:
https://social.msdn.microsoft.com/Forums/en-US/f4a4f73b-3e13-40bf-99df-9c1bba8fe44e/include-existing -folder-path-as-project-item?forum=vsx
如果该文件夹实际不存在,您可以使用
AddFolder(folderName)
。 但是,如果该文件夹在物理存在时未包含在项目中,则需要提供该文件夹的完整系统路径。 (添加文件夹(完整路径)
)I am developing an extension for Visual Studio 2019 and had a similar issue. The question asked in the following page helped me out:
https://social.msdn.microsoft.com/Forums/en-US/f4a4f73b-3e13-40bf-99df-9c1bba8fe44e/include-existing-folder-path-as-project-item?forum=vsx
If the folder does not physically exist, you can use
AddFolder(folderName)
. But if the folder is not included in the project while existing physically, you need to provide the full system path to the folder. (AddFolder(fullPath)
)这是我想到的一个想法,因为我已经使用 NAnt 很长时间了,并且认为它可能有效。
在文本编辑器中打开 .csproj 文件并添加目录,如下所示:
如果“ItemGroup”已存在,那就没问题。 只需将其添加到现有的中即可。 Visual Studio 并不真正知道如何编辑此条目,但它会扫描整个目录。
编辑成你想要的任何内容。
here's an idea i thought of because i've been using NAnt for so long and thought it might work.
Open the .csproj file in a text editor and add the directory as such:
if an "ItemGroup" already esists, that's fine. Just add it into an existing one. Visual studio won't really know how to edit this entry, but it will scan the whole directory.
edit to whatever you'd like.