C# VS2008 - 为调试/发布配置输出不同的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1093338/
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
VS2008 - Outputting a different file name for Debug/Release configurations
提问by xyz
When building a C# application with Visual Studio 2008, is it possible to set a different output filename per configuration?
使用 Visual Studio 2008 构建 C# 应用程序时,是否可以为每个配置设置不同的输出文件名?
e.g.
MyApp_Debug.exe
MyApp_Release.exe
I tried a post-build step to rename the file by appending the current configuration, but that seems a scrappy approach. Plus it meant that Visual Studio could no longer find the file when pressing F5 to start debugging.
我尝试了一个构建后步骤,通过附加当前配置来重命名文件,但这似乎是一种杂乱无章的方法。此外,这意味着 Visual Studio 在按 F5 开始调试时无法再找到该文件。
采纳答案by adrianbanks
You can achieve this by editing your project file by hand. Locate the <AssemblyName>
node and add a conditional attribute to it:
您可以通过手动编辑项目文件来实现此目的。找到<AssemblyName>
节点并为其添加条件属性:
<AssemblyName Condition="'$(Configuration)'=='Debug'">MyApp_Debug.exe</AssemblyName>
<AssemblyName Condition="'$(Configuration)'=='Release'">MyApp_Release.exe</AssemblyName>
You'll have to duplicate it also to add another conditional attribute for the release version.
您还必须复制它才能为发布版本添加另一个条件属性。
Whilst it is possible, it may cause problems. There is an AssemblyConfigurationattribute that can be applied to your assembly. In AssemblyInfo.cs
, put:
虽然这是可能的,但它可能会导致问题。有一个可以应用于您的程序集的AssemblyConfiguration属性。中AssemblyInfo.cs
,输入:
#if DEBUG
[assembly: AssemblyConfiguration("Debug")]
#else
[assembly: AssemblyConfiguration("Release")]
#endif
This will add a property to your compiled assembly that will tell you which build configuration your application was built using.
这将向已编译的程序集添加一个属性,该属性将告诉您应用程序是使用哪种构建配置构建的。
回答by Justin
I'm sure there is, however in my experience having different filenames for debug / release configurations is a bad idea as it can cause all sorts of problems (very much like the issue VS has when it tries to execute the renamed app)
我确定有,但是根据我的经验,为调试/发布配置使用不同的文件名是一个坏主意,因为它可能会导致各种问题(非常像 VS 在尝试执行重命名的应用程序时遇到的问题)
Why not simply indicate whether or not its debug / release in the Assembly attributes (for example in the comments)
为什么不简单地在 Assembly 属性中指明它是否调试/发布(例如在评论中)
回答by Christopher Karper
As adrianbanks mentioned, you can edit your .csproj file by hand to accomplish this.
正如 adrianbanks 所提到的,您可以手动编辑 .csproj 文件来完成此操作。
I would, however reccomend the simpler form of:
但是,我会推荐更简单的形式:
<AssemblyName>MyApp_$(Configuration).exe</AssemblyName>
If you ever edit the properties of this project however, this change will very likely be lost. It's something you will have to manually stay on top of, as it's not going to be a supported setup.
但是,如果您曾经编辑过此项目的属性,则此更改很可能会丢失。这是您必须手动掌握的东西,因为它不会成为受支持的设置。
To manually edit your project definition, right click the project in Visual Studio, and select "Unload", then right click the unloaded project, and select "Edit" and it will open the XML definition for you.
要手动编辑您的项目定义,请在 Visual Studio 中右键单击该项目,然后选择“卸载”,然后右键单击卸载的项目并选择“编辑”,它将为您打开 XML 定义。