C# 使用文件::的条件存在且不工作

C# 使用文件::的条件存在且不工作,c#,msbuild,msbuild-4.0,C#,Msbuild,Msbuild 4.0,我当前创建了第一个MSBuild脚本 我有一个标记“Folders”,它查找给定根路径中的所有目录: <?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> <Target Name="Build"> &l

我当前创建了第一个MSBuild脚本

我有一个标记“Folders”,它查找给定根路径中的所有目录:

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <Target Name="Build">
    <PropertyGroup>
      <RootFolder>tmp</RootFolder>
    </PropertyGroup>
    <ItemGroup>
      <Folders Include="$([System.IO.Directory]::GetDirectories(&quot;$(RootFolder)&quot;))"/>
    </ItemGroup>
    <Message Text="@(Folders -> '%(FullPath)\Bin\Debug\%(Filename)%(Extension).dll', ';')"/>
  </Target>
</Project>
用于文件夹标记

此脚本运行时没有任何错误,但我的列表为空。 为什么?

是否有其他解决方案来检查文件

我使用这个解决方案是因为它使用C语言,而我是一个C语言开发人员

<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <Target Name="Build">
    <PropertyGroup>
      <RootFolders>tmp</RootFolders>
    </PropertyGroup>
    <GetFiles rootFolders="$(RootFolders)">
      <Output PropertyName="Files" TaskParameter="Files" />
    </GetFiles>
    <Message Text="$(Files)" />
  </Target>

  <UsingTask
      TaskName="GetFiles"
      TaskFactory="CodeTaskFactory"
      AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
      <ParameterGroup>
        <rootFolders ParameterType="System.String" Required="true" />
        <files ParameterType="System.String" Output="true" />
      </ParameterGroup>
      <Task>
          <Using Namespace="System" />
          <Using Namespace="System.IO" />
          <Using Namespace="System.Linq" />
          <Code Type="Fragment" Language="cs">
              <![CDATA[               
                  Func<string, string> BuildFilePath = path => path + @"\Bin\Debug\" + Path.GetFileName(path) + ".dll";
                  var dirs = Directory.GetDirectories(rootFolders).Where(x => File.Exists(BuildFilePath(x)));
                  files = string.Join("\n", dirs.Select(BuildFilePath));
              ]]>
          </Code>
      </Task>
  </UsingTask>
</Project>

tmp

BuildFilePath=path=>path++“\Bin\Debug\”+path.GetFileName(path)+.dll”;
var dirs=Directory.GetDirectories(rootFolders).Where(x=>File.Exists(BuildFilePath(x));
files=string.Join(“\n”,dirs.Select(BuildFilePath));
]]>


AFAIK,执行并检查整个项目声明(即
标记)的
条件

我认为,您需要在集合中循环(例如,使用目标/任务批处理),并检查该文件是否存在于集合中的每个文件夹中。然后,如果文件存在-将其包含在新的
项目集合中

注意:我现在没有时间测试代码,但大致是这样:

<Target Name="FilterFolders"
    Inputs="@(Folders)"
    Outputs="%(FullPath)">
    <ItemGroup>
    <FoldersFiltered Include="@(Folders->'%(FullPath)')"
        Condition="$([System.IO.File]::Exists(&quot;@(Folders->'%(FullPath)'\\Bin\\Debug\\YourFile.dll&quot;))" />
    </ItemGroup>
</Target>

“%(完整路径)\\Bin\\Debug\\YourFile.dll”)”/>
Thx,但我是一名C#开发人员,找到了内联代码的解决方案。现在我可以用C#做所有的工作了。
<Target Name="FilterFolders"
    Inputs="@(Folders)"
    Outputs="%(FullPath)">
    <ItemGroup>
    <FoldersFiltered Include="@(Folders->'%(FullPath)')"
        Condition="$([System.IO.File]::Exists(&quot;@(Folders->'%(FullPath)'\\Bin\\Debug\\YourFile.dll&quot;))" />
    </ItemGroup>
</Target>