visual-studio 我想删除所有 bin 和 obj 文件夹以强制所有项目重建所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/755382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:22:36  来源:igfitidea点击:

I want to delete all bin and obj folders to force all projects to rebuild everything

visual-studiovisual-studio-2008batch-filecmd

提问by MichaelD

I work with multiple projects, and I want to recursively delete all folders with the name 'bin' or 'obj' that way I am sure that all projects will rebuild everything (sometimes it's the only way to force Visual Studio to forget all about previous builds).

我处理多个项目,我想以这种方式递归删除名为“bin”或“obj”的所有文件夹,我确信所有项目都会重建所有内容(有时这是强制 Visual Studio 忘记所有以前的唯一方法)构建)。

Is there a quick way to accomplish this (with a .bat file for example) without having to write a .NET program?

是否有一种无需编写 .NET 程序即可快速完成此操作的方法(例如,使用 .bat 文件)?

回答by Steve Willcock

This depends on the shell you prefer to use.

这取决于您喜欢使用的外壳。

If you are using the cmd shell on Windows then the following should work:

如果您在 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"

If you are using a bash or zsh type shell (such as git bash or babun on Windows or most Linux / OS X shells) then this is a much nicer, more succinct way to do what you want:

如果您使用的是 bash 或 zsh 类型的 shell(例如 Windows 上的 git bash 或 babun 或大多数 Linux / OS X shell),那么这是一种更好、更简洁的方式来执行您想要的操作:

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

and this can be reduced to one line with an OR:

这可以用 OR 减少到一行:

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

Note that if your directories of filenames contain spaces or quotes, find will send those entries as-is, which xargs may split into multiple entries. On macOS, the workaround is to use -print0 and -0 so the above examples become:

请注意,如果您的文件名目录包含空格或引号, find 将按原样发送这些条目, xargs 可能会拆分为多个条目。在 macOS 上,解决方法是使用 -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

If you are using Powershell then you can use this:

如果您使用的是 Powershell,那么您可以使用它:

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

as seen in Robert H's answer below- just make sure you give him credit for the powershell answer rather than me if you choose to up-vote anything :)

下面的 Robert H 的回答所示-如果您选择对任何内容进行投票,请确保您将他的功劳归功于 powershell 答案,而不是我:)

It would of course be wise to run whatever command you choose somewhere safe first to test it!

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

回答by Robert H.

I found this thread and got bingo. A little more searching turned up this power shell script:

我找到了这个线程并得到了宾果游戏。多一点搜索发现了这个 power shell 脚本:

Get-ChildItem .\ -include bin,obj -Recurse | ForEach-Object ($_) { Remove-Item $_.FullName -Force -Recurse }

I thought I'd share, considering that I did not find the answer when I was looking here.

我想我会分享,因为我在这里看时没有找到答案。

回答by Daniel Rehner

This worked for me:

这对我有用:

for /d /r . %%d in (bin,obj) do @if exist "%%d" rd /s/q "%%d"

Based on this answeron superuser.com

基于superuser.com上的这个答案

回答by Jhonny D. Cano -Leftware-

I use to always add a new target on my solutions for achieving this.

我过去总是在我的解决方案中添加一个新目标来实现这一目标。

<Target Name="clean_folders">
  <RemoveDir Directories=".\ProjectName\bin" />
  <RemoveDir Directories=".\ProjectName\obj" />
  <RemoveDir Directories="$(ProjectVarName)\bin" />
  <RemoveDir Directories="$(ProjectVarName)\obj" />
</Target>

And you can call it from command line

你可以从命令行调用它

msbuild /t:clean_folders

This can be your batch file.

这可以是您的批处理文件。

msbuild /t:clean_folders
PAUSE

回答by doblak

I wrote a powershell scriptto do it.

我写了一个powershell脚本来做到这一点。

The advantage is that it prints out a summary of deleted folders, and ignored ones if you specified any subfolder hierarchy to be ignored.

优点是它会打印出已删除文件夹的摘要,如果您指定要忽略的任何子文件夹层次结构,则会打印出被忽略的文件夹。

Output example

输出示例

回答by Kdefthog

Nothing worked for me. I needed to delete all files in bin and obj folders for debug and release. My solution:

没有什么对我有用。我需要删除 bin 和 obj 文件夹中的所有文件以进行调试和发布。我的解决方案:

1.Right click project, unload, right click again edit, go to bottom

1.右键项目,卸载,再次右键编辑,到底部

2.Insert

2.插入

<Target Name="DeleteBinObjFolders" BeforeTargets="Clean">
  <RemoveDir Directories="..\..\Publish" />
  <RemoveDir Directories=".\bin" />
  <RemoveDir Directories="$(BaseIntermediateOutputPath)" />
</Target>

3. Save, reload project, right click clean and presto.

3.保存,重新加载项目,右键点击clean and presto。

回答by vezenkov

Something like that should do it in a pretty elegant way, after clean target:

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

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

回答by Shaman

To delete bin and obj before build add to project file:

要在构建之前删除 bin 和 obj 添加到项目文件:

<Target Name="BeforeBuild">
    <!-- Remove obj folder -->
    <RemoveDir Directories="$(BaseIntermediateOutputPath)" />
    <!-- Remove bin folder -->
    <RemoveDir Directories="$(BaseOutputPath)" />
</Target>

Here is article: How to remove bin and/or obj folder before the build or deploy

这是文章:如何在构建或部署之前删除 bin 和/或 obj 文件夹

回答by Aboo

Very similar to Steve's PowerShell scripts. I just added TestResults and packages to it as it is needed for most of the projects.

非常类似于史蒂夫的 PowerShell 脚本。我刚刚添加了 TestResults 和包,因为大多数项目都需要它。

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

回答by Vedran Mandi?

A very quick and painless way is to use the rimrafnpm utility, install it globally first:

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

> npm i rimraf -g

And then the command from your project root is quite simple (which can be saved to a script file):

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

projectRoot> rimraf **/bin **/obj

To optimize the desired effect you can leverage the project event targets (the one you could use is BeforeRebuildand make it run the previous command) which are specified in the docs: https://docs.microsoft.com/en-us/visualstudio/msbuild/how-to-extend-the-visual-studio-build-process?view=vs-2017

为了优化所需的效果,您可以利用BeforeRebuild文档中指定的项目事件目标(您可以使用的是并使其运行上一个命令):https: //docs.microsoft.com/en-us/visualstudio/ msbuild/how-to-to-extend-the-visual-studio-build-process?view=vs-2017

I like the rimraf utility as it is crossplat and really quick. But, you can also use the RemoveDircommand in the .csproj if you decide to go with the target event option. The RemoveDirapproach was well explained in another answer here by @Shaman: https://stackoverflow.com/a/22306653/1534753

我喜欢 rimraf 实用程序,因为它是跨平台的,而且速度非常快。但是,RemoveDir如果您决定使用目标事件选项,您也可以使用.csproj 中的命令。该RemoveDir方法在@Shaman 的另一个答案中得到了很好的解释:https://stackoverflow.com/a/22306653/1534753