projectitem.AddFromDirectory不能与VS2015一起工作
本文关键字:一起 工作 VS2015 AddFromDirectory 不能 projectitem | 更新日期: 2023-09-27 18:07:01
我需要以编程方式复制目录到我的项目。我在我的项目中使用了下面的代码:
ProjectItems.AddFromDirectory(_projectPath + "''Folder");
但是它不会复制整个目录。它只是将根文件夹添加到项目中。我独自面对VS2015。
有解决方案吗?
这可能不是最优雅的解决方案。但它正在按要求使用AddFromDirectory。我使用Visual Studio 2015 Professional进行了测试。
public static void RefreshItemsInProject(Project project, string projectPath)
{
foreach(string subDirectoryPath in Directory.EnumerateDirectories(projectPath))
AddItemsToProjectFromDirectory(project.ProjectItems, subDirectoryPath));
}
private static readonly HashSet<string> _excluded = new HashSet<string>() { "bin", "obj" };
public static void AddItemsToProjectFromDirectory(ProjectItems projectItems, string directoryPath)
{
//very weird GetDirectoryName returns the FULL PATH!!!!
//When using GetFileName on a Directory it actually returns the FolderName WITHOUT the PATH.
var directoryName = Path.GetFileName(directoryPath);
if(_excluded.Contains(directoryName.ToLower()))//folder to exclude like bin and obj.
return;//return right away if the folder has been excluded.
var subFolder = projectItems.AddFromDirectory(directoryPath);
foreach(string subDirectoryPath in Directory.EnumerateDirectories(directoryPath))
AddItemsToProjectFromDirectory(subFolder.ProjectItems, subDirectoryPath);
}
像这样使用这个代码:
RefreshItemsInProject(myProject, projectPath);
myProject的类型是EnvDTE。项目
你可以在项目中每次有变化时调用它;比如添加了一个新文件;它将刷新项目的内容。
请注意,它支持"排除"目录列表,以避免在项目中包含bin和obj之类的内容。
我希望这能帮助到一些人。
UPDATE:我意识到这只适用于二级目录。这样的:
MyDir->MySecondDir->MyItems THIS WORKS!第二层的项目。
MyDir->MyItems这行不通!第一关的物品
由于某些原因,它无法在第一级目录中添加项目。看起来像是微软实现中的一个bug。如果有人能解释一下这个问题;我真的很感激。