0

My Visual Studio 2013 C# app needs to programmatically determine if another Visual Studio solution has been saved in "build mode" or "release mode". Is there an API for accomplishing this?

Alternatively, I was thinking of using the MSBuild API to build the solution and then check to see if the app has debug symbols. Is there a way of doing this?

  • You could declare a symbol within a "#if DEBUG" then inspect that symbol via reflection. – Moby Disk Jul 24 '14 at 20:01
  • @MobyDisk: That's a pretty cool idea, but this has to work across the entire company for all samples that we ship. Therefore, I'm hoping for a solution that won't require all sample writers to update their code for my app to function. – Strax Tillbaka Jul 24 '14 at 20:03

3 Answers3

2

Debug, Release or any other custom configuration are just names, anyone can make one to look like the other one in Advanced Build Settings of Project Properties or by tweaking properties in the .csproj directly. Those flags will dictate how you identify a "debug" assembly and how much of "debug" debug really means. That said, the difference in output for default combination of properties is in Debuggable assembly attribute, both default configurations have it and you can see it for yourself using ILSpy.

Debug

[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default
 | DebuggableAttribute.DebuggingModes.DisableOptimizations
 | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints
 | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]

Release

[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]

On how to read this Debuggable attribute value -- see @MobyDisk answer.

Ilya Kozhevnikov
  • 10,242
  • 4
  • 40
  • 70
1

When you build an assembly in debug mode, the compiler adds the [assembly: DebuggableAttribute] automatically. You can use reflection to see if that attribute is present on the assembly. Take a look here for details on how to read attributes from an assembly: How to read assembly attributes

Community
  • 1
  • 1
Moby Disk
  • 3,761
  • 1
  • 19
  • 38
1

I'm currently solving the same problem using Web.config:

My Web.config (as default this will be equivalent to Web.Debug.config and Web.Development.config) file contains (using your naming above):

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="mode" value="build"/>
    <add key="anotherKey" value="another value"/>
  </appSettings>
</configuration>

My Web.Test.config and Web.Release.config files contain:

 <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <appSettings>
    <add key="mode" value="release" xdt:Transform="Replace" xdt:Locator="Match(key)"/>
  </appSettings>
</configuration>

Then you can access the 'mode' using:

WebConfigurationManager.AppSettings.Get("mode");

[you don't have to use 'mode' for the key, as long as you are consistent]

edson-
  • 123
  • 1
  • 8