How do I get the #if DEBUG to work?
I have a simple application:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
#If DEBUG Then
lblDebug.Text = "Debug"
#Else
lblDebug.Text = "Not in debug"
#End If
Now when I run it in VS, it prints "debug" like it shou开发者_如何学运维ld, but if i run the .exe, it still prints "debug". What do i have to do to get this to work right?
It's already working "right." The way to make it work the way you think it should is to change your Build Configuration from Debug to Release. Then, when you run it in Visual Studio (or from the executable file) it will say "Not in debug" the way you expect.
Now, if you're more interested in checking at runtime to see if there's a debugger attached to the application, you can use Debugger.IsAttached
to see if the program is currently being debugged.
Build the exe in Release mode (select it from the dropdown at the top of Visual Studio).
Change your "Solution Configuration" to Release and build it again.
精彩评论