我与多个项目一起工作,我想递归删除所有名为“bin”或“obj”的文件夹,这样我就可以确保所有项目都会重新构建所有内容(有时这是迫使Visual Studio忘记所有以前的构建的唯一方法)。

是否有一种快速的方法来实现这一点(例如使用.bat文件),而不必编写。net程序?


当前回答

这对我来说很好: 开始/d /r。%%d in (bin,obj, ClientBin,Generated_Code) do @if exist "%%d" rd /s /q "%%d"

其他回答

在VS 2019/VS 2022中,这是唯一明智的解决方案。

在解决方案文件夹(.sln文件所在的位置)中,创建一个名为Directory.Build.props的文件,并按如下所示添加编辑它。在这里阅读这个特殊的文件。

Directory.Build.props

<Project>
  <Target Name="RemoveObjAndBinFolders" AfterTargets="Clean">
    <PropertyGroup>
      <ObjFolder>$(ProjectDir)$(BaseIntermediateOutputPath)</ObjFolder>
      <BinFolder>$(ProjectDir)$(BaseOutputPath)</BinFolder>

      <!-- Microsoft.NET.Sdk.Web sets $(BaseIntermediateOutputPath) to -->
      <!-- an absolute path. Not fixed up to MsBuild 17! -->
      <BaseIntermediateOutputPathFix Condition="$(BaseIntermediateOutputPath.StartsWith($(MSBuildProjectDirectory)))">$([MSBuild]::MakeRelative(
        $(ProjectDir),
        $(BaseIntermediateOutputPath)
      ))</BaseIntermediateOutputPathFix>
    
      <ObjFolder Condition="$(BaseIntermediateOutputPath.StartsWith($(MSBuildProjectDirectory)))">$(ProjectDir)$(BaseIntermediateOutputPathFix)</ObjFolder>
    </PropertyGroup>

    <ItemGroup>
      <ObjFiles Include="$(ObjFolder)/*.*"
                Exclude="$(ObjFolder)/project.assets.json" />
      <ObjSubFolders
                Include="$([System.IO.Directory]::GetDirectories('$(ObjFolder)'))" />
    </ItemGroup>
    
    <!-- Remove "obj" sub folders -->
    <RemoveDir Directories="@(ObjSubFolders)" ContinueOnError="true" />
    <!-- Remove "obj" files (keeping necessary asset file)-->
    <Delete Files="@(ObjFiles)" />
    
    <!-- Remove "bin" folders -->
    <RemoveDir Directories="$(BinFolder)" ContinueOnError="true" />
  </Target>
</Project>

不需要修改一堆.csproj文件。另外,请注意,我并没有像一些人建议的那样删除$(TargetDir)。如果$(OutDir)被设置为某个自定义目录(一种常见的做法),那么这样做可能会使构建系统瘫痪。

从使用Windows PowerShell删除obj, bin和ReSharper文件夹

与Robert H的答案非常相似,只是语法更短

powershell运行 Cd(更改目录)到项目文件夹的根目录 粘贴并运行下面的脚本 Dir .\ -include bin,obj,resharper* -递归| foreach($) {rd $_。fullname -Recurse -Force}

这取决于您更喜欢使用的shell。

如果你在Windows上使用cmd shell,那么以下命令应该可以工作:

FOR /F "tokens=*" %%G IN ('DIR /B /AD /S bin') DO RMDIR /S /Q "%%G"
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S obj') DO RMDIR /S /Q "%%G"

如果你正在使用bash或zsh类型的shell(例如Windows或大多数Linux / OS X shell上的git bash或babun),那么这是一种更好、更简洁的方式来做你想做的事情:

find . -iname "bin" | xargs rm -rf
find . -iname "obj" | xargs rm -rf

这可以简化为一行带有OR的语句:

find . -iname "bin" -o -iname "obj" | xargs rm -rf

注意,如果您的文件名目录包含空格或引号,find将按原样发送这些条目,xargs可能将其拆分为多个条目。如果你的shell支持它们,-print0和-0将解决这个缺点,所以上面的例子变成:

find . -iname "bin" -print0 | xargs -0 rm -rf
find . -iname "obj" -print0 | xargs -0 rm -rf

and:

find . -iname "bin" -o -iname "obj" -print0 | xargs -0 rm -rf

如果你正在使用Powershell,那么你可以使用这个:

Get-ChildItem .\ -include bin,obj -Recurse | foreach ($_) { remove-item $_.fullname -Force -Recurse }

如下面的Robert H的回答所示——如果你选择给任何东西投赞成票,请确保你把功劳归功于他,而不是我:)

当然,明智的做法是先在安全的地方运行您选择的任何命令来测试它!

一个非常快速和轻松的方法是使用rimraf npm实用程序,首先全局安装它:

> npm i rimraf -g

然后项目根目录下的命令非常简单(可以保存到脚本文件中):

projectRoot> rimraf **/bin **/obj

为了优化想要的效果,你可以利用文档中指定的项目事件目标(你可以使用的是BeforeRebuild,并让它运行前面的命令):https://learn.microsoft.com/en-us/visualstudio/msbuild/how-to-extend-the-visual-studio-build-process?view=vs-2017

我喜欢rimraf实用程序,因为它是十字板,真的很快。但是,如果您决定使用目标事件选项,也可以在.csproj中使用RemoveDir命令。RemoveDir方法在@Shaman的另一个回答中有很好的解释:https://stackoverflow.com/a/22306653/1534753

注意:这是一个老答案,可能需要在VS 2019和一些obj工件的新版本中进行调整。在使用这种方法之前,请确保VS不需要在目标和输出目录中成功构建任何东西。

像这样的东西应该以一种非常优雅的方式完成,在干净的目标之后:

<Target Name="RemoveObjAndBin" AfterTargets="Clean">
    <RemoveDir Directories="$(BaseIntermediateOutputPath)" />
    <RemoveDir Directories="$(TargetDir)" />
</Target>