El TecnoBaúl de Kiquenet

Kiquenet boring stories

Archive for 1 diciembre 2010

Detecting if Code is Running from the IDE

Posted by kiquenet en 1 diciembre 2010

During development, you may find that you need your code to determine if is running from the IDE or standalone.

Unlike previous Microsoft development platforms like VB 6, .NET assemblies don’t have a different “mode” when running from the IDE or from the command line. This can make it difficult to figure out how to determine if an assembly is running from the IDE (i.e. from within Visual Studio .NET).

There are several methods of accomplishing this. The simplest is to use the System.Diagnostics.Debugger.IsAttached property to determine if there is a .NET debugger attached to the currently executing code. If there is, you can assume the code is running from the IDE:

// C#

if (Debugger.IsAttached = true)

      {

// Since there is a debugger attached,

// assume we are running from the IDE

      }

else

      {

// Assume we aren’t running from the IDE

      }

‘ Visual Basic.NET

If Debugger.IsAttached Then

‘ Since there is a debugger attached,

‘ assume we are running from the IDE

Else

‘ Assume we aren’t running from the IDE

End If

User Controls in Runtime Mode

There are also issues with creating your own controls. In some cases, you have code that should not execute in design mode, but should in run mode. In this case, simply use the DesignMode property of the object:

// C#

if (this.DesignMode() = true)

      {

// The component is in design mode

      }

else

      {

// The component is in run mode

      }

‘ Visual Basic.NET

If Me.DesignMode Then

‘ The component is in design mode

Else

‘ The component is in run mode

End If

http://www.fmsinc.com/free/NewTips/NET/NETtip32.asp

Anuncios

Posted in .NET, Trucos | Etiquetado: , | Leave a Comment »