有没有办法自动包含内容文件到asp.net项目文件?

我经常向ASP.NET项目中添加大量的内容文件(主要是图片和js)。 我正在使用VS发布系统,在发布时,只有在将它们包含在项目中时才会发布新文件。 我想自动包含指定目录中的所有文件。 有没有办法指定哪些目录应自动包含在csproj文件或其他地方?

我知道,老线程,但我find了一个方法来做到这一点,我一直忘记,并在我的searchfind最后一次,我偶然发现这个问题。 我发现的最好的方法是在.csproj文件中使用BeforeBuild目标。

<Target Name="BeforeBuild"> <ItemGroup> <Content Include="**\*.less" /> </ItemGroup> </Target> 

VS 2010不会涉及这一部分,它确保您的文件在每次构build项目时都被包含为内容。

你只需要扩展你的网站.csproj文件。 只需添加一个recursion通配符的内容根文件夹:

 ... <ItemGroup> <!-- your normal project content --> <Content Include="Default.aspx" /> <!-- your static content you like to publish --> <Content Include="Images\**\*.*" /> </ItemGroup> ... 

这样做使得这个文件夹和下面的所有内容在您的解决scheme浏览器中可见。

如果您尝试通过指定来隐藏解决scheme浏览器内的文件夹

 <Content Include="Images\**.*.*"> <Visible>false</Visible> </Content> 

它不会被公布。


更新

正如您已经发现,只要触摸解决scheme中的文件夹,通配符就会被replace,因为VS项目并不包含任意内容。

所以你必须确保文件夹及其内容不会在VS内被修改 – 添加或删除文件只能在文件系统上完成…这是你想要的,因为我理解你的问题。

如果该文件夹可以隐藏在VS中会更容易,但是我找不到隐藏它并发布的方法。

另一个不成功的方法是通过CreateItem任务包含文件夹。 这导致文件夹的内容被发布到\ bin \ app.publish \ …,并且无法确信将它与.csproj中的内容项一起发布,所以我没有在我的答案中提供它。

您可以使用框架的System.IO.Directory.GetFile(string)方法及其重载recursion包含所有文件。

  <ItemGroup> <Content Include="$([System.IO.Directory]::GetFiles('$(ProjectDir)Scripts\', '*.js', SearchOption.AllDirectories))" /> <Content Include="$([System.IO.Directory]::GetFiles('$(ProjectDir)Images\', '*.png', SearchOption.AllDirectories))" /> </ItemGroup> 

我已经写了我是如何获得包含一个小的powershell脚本创build的内容:

 $folders = Get-ChildItem .\ -r -Directory $filtered = $folders |Select-Object @{Name='FullName';Expression={$_.fullname.replace($pwd,'')}}, @{Name='FolderDepth';Expression={($_.fullname.Split('\').Count) - ($Pwd.Path.split('\').count)}} | Sort-Object -Descending FullName,folderDepth $basefolders = $filtered | Where-Object{$_.folderdepth -eq 1} $basefoldersobj = @() foreach($basefolder in $basefolders) { $basefoldername =$baseFolder.fullname $filteredbase = $filtered -match "\$basefoldername\\" | Sort-Object -Descending FolderDepth | Select-Object -first 1 if($filteredbase -eq $null) { $filteredbase = $filtered -match "\$basefoldername" | Sort-Object -Descending FolderDepth | Select-Object -first 1 } $obj = New-Object psobject Add-Member -InputObject $obj -MemberType NoteProperty -Name 'Folder' -Value $basefolder.fullname.trim('\') Add-member -InputObject $obj -MemberType NoteProperty -Name 'DeepestPath' -Value $filteredbase.folderDepth $basefoldersobj += $obj } $include = '*.*' foreach($bfolderObj in $basefoldersobj) { $includecount = '' $includecount = "\$include" * ($bfolderObj.Deepestpath) Write-Output "<content Include=`"$($bfolderObj.folder)$includecount`" /> " } 

这应该在PowerShell提示符下产生必要的包含声明

您可以添加像这样的链接的文件,它们是可search的,可查看的,但如果您尝试更改它们,则不会检出,Visual Studio也会将通配符留在原地:

  <ItemGroup> <Content Include="..\Database Schema\Views\*.sql"> <Link>Views\*.sql</Link> </Content> </ItemGroup> 

这里面的.proj文件。

不是我的知识; 不过我的build议是将它们粘贴到项目中,因为这将默认包含它们。 所以,而不是通过资源pipe理器将它们粘贴到目录中,使用Visual Studio将文件粘贴到文件夹中。

我意识到,最好的解决scheme是手动添加文件,一个接一个。 如果你像我一样有数百个,那只是几个小时的问题。 有趣的是,即使在2016年与VS 2015这个严重的问题仍然没有解决。 啊,我如何爱Xcode。