visual-studio 如何从我的项目中删除不必要的资源?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1496731/
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 11:03:24  来源:igfitidea点击:

How do I remove unnecessary resources from my project?

c#c++visual-studioresources

提问by Pratik Deoghare

I am working with a very big project (a solution that contains 16 projects and each project contains about 100 files).

我正在处理一个非常大的项目(一个包含 16 个项目的解决方案,每个项目包含大约 100 个文件)。

It is written in C++/C# with Visual Studio 2005.
One of the projects has around 2000 resources out of which only 400 are actually used.
How do I remove those unused resources?

它是用 C++/C# 和 Visual Studio 2005 编写的。
其中一个项目有大约 2000 个资源,其中只有 400 个实际使用。
如何删除那些未使用的资源?

I tried to accomplish the task by searching for used ones.
It worked and I was able to build the solution, but it broke at runtime.

我试图通过搜索使用过的来完成任务。
它有效并且我能够构建解决方案,但它在运行时坏了。

I guess because enums are used. (IMPORTANT)

我猜是因为使用了enum。(重要

How can I make sure that it doesn't break at runtime?

如何确保它在运行时不会中断?

EDIT:
I think one method could be to generate the resource (that is not found) on the fly at runtime (somehow).
But I have no idea about ... anything.

编辑:
我认为一种方法可能是在运行时(以某种方式)动态生成资源(未找到)。
但我不知道......任何事情

NOTE: It's okay if a few unnecessary resources are still there.

注意:如果一些不必要的资源仍然存在也没关系。

采纳答案by C Johnson

What I would do is write a custom tool to search your source code.

我要做的是编写一个自定义工具来搜索您的源代码。

If you remove a resource ID from a header file (i.e. possibly called resource.h) and then recompile and get no warnings: then that's a good thing.

如果您从头文件(即可能称为 resource.h)中删除资源 ID,然后重新编译并且没有收到警告:那么这是一件好事。

Here is how I would go about writing the app. Take as input the resource file (resource.h) you want to scrutinize. Open the header file (*.h) and parse all the resource constants (Or at least the onces you are interested in). Store those in a hash table for quick look up later. For each code file in your project, search the text for instances of each of your resource ID's. When a resource ID is used, increment the value in the hash table otherwise leave it at zero. At the end, dump all the resource ID's that are zero out a log file or something. Then test that indeed you can remove those specified resource ID's safely. Once you do that, then write another tool that removes the specified resource ID's given the results of your log file.

这是我将如何编写应用程序。将要检查的资源文件 (resource.h) 作为输入。打开头文件 (*.h) 并解析所有资源常量(或者至少是您感兴趣的一次)。将它们存储在哈希表中,以便以后快速查找。对于项目中的每个代码文件,在文本中搜索每个资源 ID 的实例。使用资源 ID 时,增加哈希表中的值,否则将其保留为零。最后,转储日志文件或其他内容为零的所有资源 ID。然后测试您确实可以安全地删除那些指定的资源 ID。一旦你这样做了,然后编写另一个工具,根据日志文件的结果删除指定的资源 ID。

You could write such a tool in perl and it would execute in about 0.3 seconds: But would take days to debug. :) Or you could write this in .NET, and it would execute a little slower, but would take you an hour to debug. :)

您可以用 perl 编写这样一个工具,它会在大约 0.3 秒内执行:但是需要几天的时间来调试。:) 或者你可以用 .NET 编写它,它的执行速度会慢一点,但需要一个小时来调试。:)

回答by Corlin

In the "Resources View" of the Solution Explorer, right-click and select "Resource Symbols". Now you get a list where you can see which resources constants are used in the .RC-file. This help you might be a bit on the way to cleanup your Resource.h (although it does not show you which resources are not used in the actual C++ code).

在解决方案资源管理器的“资源视图”中,右键单击并选择“资源符号”。现在您将获得一个列表,您可以在其中查看 .RC 文件中使用了哪些资源常量。这可能会帮助您清理 Resource.h(尽管它没有显示在实际 C++ 代码中未使用哪些资源)。

回答by Patrice Bernassola

You can use third party plug-in for Visual Studio as ReSharper. This add-in will analyze your C# code and point out unused resources. But it only works with C#.

您可以将 Visual Studio 的第三方插件用作ReSharper。此加载项将分析您的 C# 代码并指出未使用的资源。但它只适用于 C#。

回答by Lior Kogan

For C++ projects, check out The ResOrgfrom Riverblade.

对于 C++ 项目,请查看Riverblade 的The ResOrg。

"The Resource ID Organiser (ResOrg for short) is an Add-in for Visual C++ designed to help overcome one of the most annoying (and unnecessary) chores of developing/maintaining Windows applications - maintaining resource symbol ID values"

“资源 ID 管理器(简称 ResOrg)是 Visual C++ 的插件,旨在帮助克服开发/维护 Windows 应用程序中最烦人(和不必要)的杂务之一——维护资源符号 ID 值”

http://www.riverblade.co.uk/products/resorg/index.html

http://www.riverblade.co.uk/products/resorg/index.html

回答by Dave

I've never had one that bad. My method in compiled programs is to use a REXX script which emulates GREP looking for references to source that I suspect is not being used, remove them from the program and see what breaks. I use the REXX script because I can pre-filter the list of files I want to search. Which allows me to do a search across folders and computers.

我从来没有遇到过这么糟糕的事情。我在编译程序中的方法是使用 REXX 脚本,该脚本模拟 GREP 查找对我怀疑未使用的源的引用,将它们从程序中删除,看看有什么中断。我使用 REXX 脚本是因为我可以预先过滤要搜索的文件列表。这使我可以跨文件夹和计算机进行搜索。

回答by AndrewB

You may want to take a look at the tool Reflector(free), not to be confused with ReSharper(expensive). It can show you which DLLs are dependent on another. Then if you want you may be able to remove the DLL that is not being referenced by anything else. Watch out if you are using dependency injection or reflection which then could break your code without your knowledge.

您可能想看看工具Reflector(免费),不要与ReSharper(昂贵)混淆。它可以显示哪些 DLL 依赖于另一个。然后,如果您愿意,您可以删除未被其他任何内容引用的 DLL。当心您是否正在使用依赖注入或反射,这可能会在您不知情的情况下破坏您的代码。

Reflector: http://www.red-gate.com/products/reflector/.

反射器:http: //www.red-gate.com/products/reflector/

This add-in draws assembly dependency graphs and IL graphs: http://reflectoraddins.codeplex.com/Wiki/View.aspx?title=Graph.

此插件绘制程序集依赖图和 IL 图:http: //reflectoraddins.codeplex.com/Wiki/View.aspx? title=Graph 。

回答by Daniel Earwicker

If your code contains dynamic loading of resources (e.g. via strings) at runtime, then there is no way to automatically determine which resources can be safely removed from the source. A dynamic loading statement could load any resource.

如果您的代码在运行时包含动态加载资源(例如通过字符串),则无法自动确定可以安全地从源中删除哪些资源。动态加载语句可以加载任何资源。

Your best bet is to start with your trimmed down version of the app, run it, and identify which resources are missing when you test it. Then add them back in and retest.

最好的办法是从应用程序的精简版本开始,运行它,并在测试时确定缺少哪些资源。然后将它们重新添加并重新测试。

回答by Ugur Turan

For C++ resources, did you try right-clicking the project in "Resource View" and then deleting the ones which do not have a tick mark next to them? It is unsafe to delete unused dialog resources since they are referenced as "enum"s in code (like the following).

对于 C++ 资源,您是否尝试在“资源视图”中右键单击项目,然后删除旁边没有勾号的项目?删除未使用的对话框资源是不安全的,因为它们在代码中被称为“枚举”(如下所示)。

enum { IDD = IDD_ABOUTBOX };

..however for all the others it should be safe.

..但是对于所有其他人来说,它应该是安全的。

回答by Chris

In the Solution Explorer, right click and on a Referenceand click on the menu item Find Dependent Code.

解决方案资源管理器中,右键单击一个引用,然后单击菜单项Find Dependent Code

If it can't find any dependent code then you can remove this reference from the project. (The Removeoperation is also under the right-click menu.)

如果找不到任何依赖代码,则可以从项目中删除此引用。(删除操作也在右键菜单下。)

EDIT:For a large project, the Find Dependent Code operation will take a longtime. So since you have 2000 resources and most likely valueyour time this probably is nota viable option....

编辑:对于大型项目,查找相关代码操作将需要长时间。因此,由于您拥有 2000 个资源并且很可能珍惜您的时间,因此这可能不是一个可行的选择......

回答by MP24

Maybe Find Unused Resources in a .NET Solutionhelps here? Basically, you'll have to check which resources are used (e.g. by comprehensive code coverage checks) and remove the unused ones.

也许Find Unused Resources in a .NET 解决方案在这里帮助?基本上,您必须检查使用了哪些资源(例如,通过全面的代码覆盖率检查)并删除未使用的资源。

And probably you should not be afraid by using the trail-and-error approach to cleaning up.

也许您不应该害怕使用跟踪错误方法进行清理。