Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ProjectItems.AddFromDirectory不适用于VS2015_C# - Fatal编程技术网

C# ProjectItems.AddFromDirectory不适用于VS2015

C# ProjectItems.AddFromDirectory不适用于VS2015,c#,C#,我需要以编程方式将目录复制到我的项目中。我在我的项目中使用了以下代码: ProjectItems.AddFromDirectory(_projectPath + "\\Folder"); 但它不会复制整个目录。它只是将根文件夹添加到项目中。我独自面对VS2015 有什么解决方案吗?这可能不是最优雅的解决方案。 但它正在按请求使用AddFromDirectory。 我使用Visual Studio 2015 Professional对此进行了测试 public static void Refre

我需要以编程方式将目录复制到我的项目中。我在我的项目中使用了以下代码:

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);
}
myProject的类型为EnvDTE.Project

每次项目发生变化时,您都可以调用此功能;例如,添加了一个新文件;它将刷新项目的内容

请注意,它支持一个“排除”目录列表,以避免在项目中包含诸如bin和obj之类的内容

我希望这能帮助一些人

更新:我意识到这只适用于二级目录。 像这样:

MyDir-->mysecondir-->MyItems这很有效第二级中的项目

MyDir-->MyItems这不起作用第一级中的项目

由于某种原因,它无法在第一级目录中添加项。
看起来像是Microsofts实现中的一个bug。如果有人能解释一下这个问题;我非常感谢。

试试ProjectItems.AddFromDirectory(\u projectPath+“\Folder”);请检查您是否具有写入权限。这可能不起作用,单斜杠在“”中无法识别。因此,我们使用了“\\”。您是否找到了解决方案?我也面临同样的问题。嗨,这是VS15的默认行为。因此,AddFromDirectory将添加根文件夹也有此问题,但尚未看到任何建议的答案。
RefreshItemsInProject(myProject, projectPath);