如何在msbuild配置文件中获取目录名?
下面是我正在使用的简单代码。它获取目录中的所有文件夹,然后给我文件夹名称如何在msbuild配置文件中获取目录名?,msbuild,Msbuild,下面是我正在使用的简单代码。它获取目录中的所有文件夹,然后给我文件夹名称 <TestProjectFolderPath Include="$([System.IO.Directory]::GetDirectories(`$(SolutionDir)`,`*.Tests`))" /> <TestProjectFolderNames Include="@(TestProjectFolderPath->'$([System.IO.Path]::GetDirectoryName(
<TestProjectFolderPath Include="$([System.IO.Directory]::GetDirectories(`$(SolutionDir)`,`*.Tests`))" />
<TestProjectFolderNames Include="@(TestProjectFolderPath->'$([System.IO.Path]::GetDirectoryName(`$([System.IO.Path]::GetFileName(`%(Identity)`))`)',' ')" />
我需要帮助来理解正确的语法以使其正常工作。不支持在转换项时对项元数据使用属性函数。我想它可能在最新的MSBuild版本中,但我现在无法测试它。作为一种解决方法,您可以自己添加新元数据,因为它的行为类似于属性,所以对于最新的MSBuild版本,一切正常:
<ItemGroup>
<TestProjectFolderPath Include="$([System.IO.Directory]::GetDirectories(`$(SolutionDir)`,`*.Tests`))" />
<TestProjectFolderPath>
<FolderName>$([System.IO.Path]::GetFileName(`%(Identity)`))</FolderName>
</TestProjectFolderPath>
</ItemGroup>
<Message Text="@(TestProjectFolderPath->'%(FolderName)', ' ')" />
根据旧版本MSBuild的Sherry,编辑见注释,等效项代码为:
<TestProjectFolderPath Include="$([System.IO.Directory]::GetDirectories($(SolutionDir),*.Tests))">
<FolderName>$([System.IO.Path]::GetFileName(%(Identity)))</FolderName>
</TestProjectFolderPath>
我漏掉了GetDirectoryName,因为在GetFileName的结果上调用它没有什么意义。它给了我这个错误所需的属性Include为空或元素中缺少确定我解决了我的错误:$[System.IO.Path]::GetFileName%Identity-hmm,您使用的MSbuild版本是什么?你显示的代码对我不起作用,无法打印任何消息。虽然我为MSBuild 12.0和15.0.Ohh工作过的代码很有趣::Microsoft R Build Engine版本4.7.2556.0[Microsoft.NET Framework,版本4.0.30319.42000]版权所有C Microsoft Corporation。保留所有权利。好的,我没有旧的MSBuild版本要测试,但如果您在第二条注释中给出的代码正常,我会将其添加到答案中?
<TestProjectFolderPath Include="$([System.IO.Directory]::GetDirectories($(SolutionDir),*.Tests))">
<FolderName>$([System.IO.Path]::GetFileName(%(Identity)))</FolderName>
</TestProjectFolderPath>