visual-studio 如何清理 Visual Studio bin 和 obj 文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1088593/
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
How to clean Visual Studio bin and obj folders
提问by tom7
If you right click on a folder, you will see a "Clean" menu item. I assumed this would clean (remove) the obj and bin directory. However, as far as I can see, it does nothing. Is there another way? (please don't tell me to go to Windows Explorer or the cmd.exe) I'd like to remove the obj and bin folder so that I can easily zip the whole thing.
如果您右键单击一个文件夹,您将看到一个“清理”菜单项。我认为这会清理(删除)obj 和 bin 目录。但是,据我所知,它什么也没做。还有其他方法吗?(请不要告诉我去 Windows 资源管理器或 cmd.exe)我想删除 obj 和 bin 文件夹,以便我可以轻松地压缩整个内容。
回答by Newtopian
As others have responded already Clean will remove all artifacts that are generated by the build. But it will leave behind everything else.
正如其他人已经做出回应, Clean 将删除构建生成的所有工件。但它会留下其他一切。
If you have some customizations in your MSBuild project this could spell trouble and leave behind stuff you would think it should have deleted.
如果您的 MSBuild 项目中有一些自定义,这可能会带来麻烦并留下您认为应该删除的内容。
You can circumvent this problem with a simple change to your .*proj by adding this somewhere near the end :
您可以通过对 .*proj 进行简单更改来规避此问题,方法是将其添加到末尾附近:
<Target Name="SpicNSpan"
AfterTargets="Clean">
<RemoveDir Directories="$(OUTDIR)"/>
</Target>
Which will remove everything in your bin folder of the current platform/configuration.
这将删除当前平台/配置的 bin 文件夹中的所有内容。
------ Edit Slight evolution based on Shaman's answerbelow (share the votes and give him some too)
------ 根据下面萨满的回答编辑轻微的进化(分享投票并给他一些)
<Target Name="SpicNSpan" AfterTargets="Clean">
<!-- Remove obj folder -->
<RemoveDir Directories="$(BaseIntermediateOutputPath)" />
<!-- Remove bin folder -->
<RemoveDir Directories="$(BaseOutputPath)" />
</Target>
---- Edit again with parts from xDisruptorbut I removed the .vs deletion as this would be better served in a .gitignore (or equivalent)
---- 使用xDisruptor 中的部分再次编辑,但我删除了 .vs 删除,因为这在 .gitignore(或等效文件)中会更好
Updated for VS 2015.
针对 VS 2015 更新。
<Target Name="SpicNSpan" AfterTargets="Clean"> <!-- common vars https://msdn.microsoft.com/en-us/library/c02as0cs.aspx?f=255&MSPPError=-2147217396 -->
<RemoveDir Directories="$(TargetDir)" /> <!-- bin -->
<RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" /> <!-- obj -->
</Target>
He also provides a good suggestion on making the task easier to deploy and maintain if you have multiple projects to push this into.
他还提供了一个很好的建议,如果您有多个项目要推动该任务,那么如何使该任务更易于部署和维护。
If you vote this answer be sure to vote them both as well.
如果您投票此答案,请务必同时投票给他们。
回答by Ruben Bartelink
If you are using git and have a correct .gitignorein your project, you can
如果你正在使用 git 并且.gitignore在你的项目中有一个正确的,你可以
git clean -xdf --dry-run
to remove absolutely every file on the .gitignorelist, i.e. it will clean obj, and binfolders (the xtriggers this behavior)
绝对删除.gitignore列表中的每个文件,即它会清理obj和bin文件夹(x触发此行为)
回答by XDS
For Visual Studio 2015 the MSBuild variables have changed a bit:
对于 Visual Studio 2015,MSBuild 变量发生了一些变化:
<Target Name="SpicNSpan" AfterTargets="Clean"> <!-- common vars https://msdn.microsoft.com/en-us/library/c02as0cs.aspx?f=255&MSPPError=-2147217396 -->
<RemoveDir Directories="$(TargetDir)" /> <!-- bin -->
<RemoveDir Directories="$(SolutionDir).vs" /> <!-- .vs -->
<RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" /> <!-- obj -->
</Target>
Notice that this snippet also wipes out the .vs folder from the root directory of your solution. You may want to comment out the associated line if you feel that removing the .vs folder is an overkill. I have it enabled because I noticed that in some third party projects it causes issues when files ala application.config exist inside the .vs folder.
请注意,此代码段还会从解决方案的根目录中清除 .vs 文件夹。如果您觉得删除 .vs 文件夹是一种矫枉过正,您可能想要注释掉相关的行。我启用了它,因为我注意到在某些第三方项目中,当文件 ala application.config 存在于 .vs 文件夹中时,它会导致问题。
Addendum:
附录:
If you are into optimizing the maintainability of your solutions you might want to take things one step further and place the above snippet into a separate file like so:
如果您想优化解决方案的可维护性,您可能希望更进一步,将上述代码片段放入一个单独的文件中,如下所示:
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="SpicNSpan" AfterTargets="Clean"> <!-- common vars https://msdn.microsoft.com/en-us/library/c02as0cs.aspx?f=255&MSPPError=-2147217396 -->
<RemoveDir Directories="$(TargetDir)" /> <!-- bin -->
<RemoveDir Directories="$(SolutionDir).vs" /> <!-- .vs -->
<RemoveDir Directories="$(ProjectDir)$(BaseIntermediateOutputPath)" /> <!-- obj -->
</Target>
</Project>
And then include this file at the very end of each and every one of your *.csproj files like so:
然后在每个 *.csproj 文件的最后包含这个文件,如下所示:
[...]
<Import Project="..\..\Tools\ExtraCleanup.targets"/>
</Project>
This way you can enrich or fine-tune your extra-cleanup-logic centrally, in one place without going through the pains of manually editing each and every *.csproj file by hand every time you want to make an improvement.
通过这种方式,您可以在一个地方集中丰富或微调您的额外清理逻辑,而无需在每次想要进行改进时手动编辑每个 *.csproj 文件。
回答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
回答by Aran Mulholland
This site: https://sachabarbs.wordpress.com/2014/10/24/powershell-to-clean-visual-studio-binobj-folders/uses powershell to remove any bin and obj folders from the current directory and sub directories. It should be possible to run it from the root of the drive Here are quotes from the article:
此站点:https: //sachabrbs.wordpress.com/2014/10/24/powershell-to-clean-visual-studio-binobj-folders/使用 powershell 从当前目录和子目录中删除任何 bin 和 obj 文件夹。应该可以从驱动器的根目录运行它以下是文章的引用:
Here is Williams version
这是威廉姆斯版本
gci -inc bin,obj -rec | rm -rec -force
In Williams own words
用威廉姆斯自己的话
That wipes out all of the “bin” and “obj” directories in the current directory and every subdirectory. Super useful to run in your workspace directory to get to a “clean” state, especially when someone messes up and there's something that a Clean or Rebuild inside the IDE doesn't catch.
这会清除当前目录和每个子目录中的所有“bin”和“obj”目录。在您的工作区目录中运行以进入“干净”状态非常有用,尤其是当有人搞砸并且 IDE 中的 Clean 或 Rebuild 无法捕获某些内容时。
For those of you reading that may not know, PowerShell supports command aliases, here it is rewritten again not using the aliases
对于那些阅读可能不知道的人,PowerShell 支持命令别名,这里再次重写,不使用别名
Get-ChildItem -inc bin,obj -rec | Remove-Item -rec -force
NOTE : You should have this stored in a PowerShell file and place that file at the root of your solution (where the .sln file resides), and then run it when you want a proper clean (not the micky mouse one that VisualStudio does, and reports success too).
注意:您应该将其存储在 PowerShell 文件中,并将该文件放在解决方案的根目录(.sln 文件所在的位置),然后在需要适当清理时运行它(而不是 VisualStudio 所做的米老鼠,并报告成功)。
回答by Stuart
Check out Ron Jacobs fantastic open source CleanProjectIt even takes care of the zipping if you like.
查看 Ron Jacobs 出色的开源CleanProject如果您愿意,它甚至可以处理压缩。
Here is the CodePlex link
这是 CodePlex链接
回答by Fragment
You can easily find and remove binand objfolders in Far Manager.
您可以在 Far Manager 中轻松查找和删除bin和obj文件夹。
- Navigate to you solution and press Alt+F7
In search setting dialog:
- Type "bin,obj"in field "A file mask or several file masks"
- Check option"Search for folders"
- Press Enter
After the search is done, switch view to "Panel".
- Select all files (with Ctrl+A) and delete folders (press "Shift+Del")
- 导航到您的解决方案并按Alt+F7
在搜索设置对话框中:
- 在字段“一个文件掩码或多个文件掩码”中键入“bin,obj”
- 选中“搜索文件夹”选项
- 按 Enter
搜索完成后,将视图切换到“面板”。
- 选择所有文件(使用 Ctrl+A)并删除文件夹(按“Shift+Del”)
Hope it helps someone.
希望它可以帮助某人。
回答by Al CoPaine
In windows just open the explorer navigate to your SLN folder click into search field and type kind:=folder;obj--> for obj folders use CTRL+A and delete 'em - same for bin Done
在 Windows 中,只需打开资源管理器导航到您的 SLN 文件夹,单击搜索字段并输入kind:=folder;obj--> 对于 obj 文件夹,请使用 CTRL+A 并删除它们 - 与 bin 相同
No need for any tool or extra software ;)
不需要任何工具或额外的软件;)
回答by Luca Ritossa
I can't add a comment yet (no minimal reputation reached)
so I leave this reply to underline that:
我还不能添加评论(没有达到最低声誉)
所以我留下这个回复强调:
the "BeforeBuild" action with <RemoveDir Directories="$(BaseIntermediateOutputPath)" />is great but, for me, is conflicting with an Entity Framework model included into the same project.
“BeforeBuild”操作 <RemoveDir Directories="$(BaseIntermediateOutputPath)" />很棒,但对我来说,它与包含在同一项目中的实体框架模型相冲突。
The error I receive is:
我收到的错误是:
Error reading resource '{mymodel}.csdl' -- 'Could not find a part of the path '{myprojectpath}\obj\Release\edmxResourcesToEmbed\{mymodel}.csdl
I suppose, the "edmxResourcesToembed" is created before the "BeforeBuild" target action is executed.
我想,“edmxResourcesToembed”是在执行“BeforeBuild”目标操作之前创建的。
回答by eka808
Based on Joe answer, I've converted the VB code into C# :
根据 Joe 的回答,我已将 VB 代码转换为 C#:
/// <summary>
/// Based on code of VSProjCleaner tool (C) 2005 Francesco Balena, Code Archirects
/// </summary>
static class VisualStudioCleaner
{
public static void Process(string rootDir)
{
// Read all the folder names in the specified directory tree
string[] dirNames = Directory.GetDirectories(rootDir, "*.*", SearchOption.AllDirectories);
List<string> errorsList = new List<string>();
// delete any .suo and csproj.user file
foreach (string dir in dirNames) {
var files = new List<string>();
files.AddRange(Directory.GetFiles(dir, "*.suo"));
files.AddRange(Directory.GetFiles(dir, "*.user"));
foreach (string fileName in files) {
try {
Console.Write("Deleting {0} ...", fileName);
File.Delete(fileName);
Console.WriteLine("DONE");
} catch (Exception ex) {
Console.WriteLine();
Console.WriteLine(" ERROR: {0}", ex.Message);
errorsList.Add(fileName + ": " + ex.Message);
}
}
}
// Delete all the BIN and OBJ subdirectories
foreach (string dir in dirNames) {
string dirName = Path.GetFileName(dir).ToLower();
if (dirName == "bin" || dirName == "obj") {
try {
Console.Write("Deleting {0} ...", dir);
Directory.Delete(dir, true);
Console.WriteLine("DONE");
} catch (Exception ex) {
Console.WriteLine();
Console.WriteLine(" ERROR: {0}", ex.Message);
errorsList.Add(dir + ": " + ex.Message);
}
}
}
Console.WriteLine(new string('-', 60));
if (errorsList.Count == 0) {
Console.WriteLine("All directories and files were removed successfully");
} else {
Console.WriteLine("{0} directories or directories couldn't be removed", errorsList.Count);
Console.WriteLine(new string('-', 60));
foreach (string msg in errorsList) {
Console.WriteLine(msg);
}
}
}
}

