|
-
Sep 24th, 2010, 07:07 AM
#1
Thread Starter
Fanatic Member
Detect if the application is a console or WinForms application
I was wondering whether anyone know's a way to detect whether the current application is a console or a winforms application.
The reason that I want to do this is that I am developing a library with a global error handler in it and I need to know what type of application it is in order to be able to subscribe to the appropriate exception event.
Obviously I could get the user to pass in a parameter specifying what type of application it is, but I would rather deal with that automatically so its all encapsulated from the user of the class.
Thanks for any help in advance.
Satal
If you find a response helpful then remember to Rate it
Personal website Sam Jenkins
-
Sep 24th, 2010, 07:54 AM
#2
Re: Detect if the application is a console or WinForms application
From what I can see, you should just be able to handle the AppDomain.UnhandledException event and then, in any WinForms app, call Application.SetUnhandledExceptionMode and specify that no exceptions are caught by the AppDomain.UnhandledException event.
-
May 2nd, 2013, 07:39 PM
#3
New Member
Re: Detect if the application is a console or WinForms application
I have a Library to Log Exception information that I use for ASP.NET Webs, Console Applications and Windows Forms ... The following code returns the exception logging file (as a FileInfo object) and shows how to automatically distinguish between ASP.NET Web callers versus either Console Applications or Windows Forms:
' Return a FileInfo pointing the fully qualified name and location based on the 'fname' input variable for saving Exception Logging information.
Private Shared Function LogPath(fname As String) As FileInfo
' Initialize the 'fi' variable to Nothing
Dim fi As FileInfo = Nothing
Try
' Has the Private LogDI DirectoryInfo variable been initialized?
If LogDI Is Nothing Then
' LogDI needs to be initialized, so determine if the caller is from ASP.NET Web or Console Application / Windows Form
If HttpContext.Current IsNot Nothing Then
' Since there is a Web Context, use the Application's 'App_Data' folder for saving the Exception Logging information
LogDI = New DirectoryInfo(HttpContext.Current.Server.MapPath(Nothing) & "/App_Data")
Else
' Since the caller is either Console Application or Windows Form, save the Exception Logging information in My.Application.Info.DirectoryPath
LogDI = New DirectoryInfo(My.Application.Info.DirectoryPath)
End If
' Make sure the Directory where the Exception Logging information is saved actually Exists
If Not LogDI.Exists Then
' The Directory needs to be created
LogDI.Create()
End If
End If
' Use the Directory specified by the LogDI variable along with the 'fname' variable passed as an input parameter to determine the FileInfo variable to Return
fi = New FileInfo(LogDI.FullName & "\" & fname)
Catch ex As Exception
' Since this code is intended to handle Exceptions passed by the User ... Oops ... What to do when this code is the cause of the Exception ... TBD
End Try
' Return the 'fi' variable in response to the caller's request
Return fi
End Function
The code shown above is called is part of a DLL that is named 'ExceptionLogging'. Here is how to call the ExceptionLogging 'LogException' procedure that will log information about a general Exception found in the 'calling code' as part of the Catch code in a Try-Catch statement ... The first LogException parameter is the Exception that has been caught and the second parameter is specified by the calling procedure to identify where the Exception was caught:
Try
...
Catch ex As Exception
ExceptionUtility.LogException(ex, "Some_WebPage_Or_ConsoleApp_Or_WinForm.vb:Procedure_With_TryCatch_Logic")
End Try
As part the logic contained in the LogException procedure, the following code shows how to distinguish different types of specific Exceptions:
Select Case True
Case TypeOf ex Is ArgumentException
Case TypeOf ex Is HttpException
Case TypeOf ex Is NullReferenceException
Case TypeOf ex Is SqlException
Case Else
End Select
Hopefully, that will offer some ideas in regard to your post.
Best Regards,
Dean
-
May 3rd, 2013, 06:51 AM
#4
Re: Detect if the application is a console or WinForms application
I used this to detect if console or form
Code:
Console.WriteLine("Press any key to continue")
Try
Console.ReadKey()
Catch ex As Exception
'here if a Form
Debug.WriteLine(ex.Message)
End Try
-
May 3rd, 2013, 07:18 AM
#5
Re: Detect if the application is a console or WinForms application
you could use reflection:
Code:
MsgBox((From t As Type In System.Reflection.Assembly.GetCallingAssembly.GetTypes() _
Where t.BaseType Is GetType(System.windows.forms.Form)).Count())
you need a reference to System.windows.forms in any projects you use it in.
in a console app. it'll always return 0, + a winforms app will be > 0
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 3rd, 2013, 08:19 AM
#6
Re: Detect if the application is a console or WinForms application
You guys do realise that this thread is two and a half years old, right?
-
May 3rd, 2013, 08:50 AM
#7
Re: Detect if the application is a console or WinForms application
 Originally Posted by jmcilhinney
You guys do realise that this thread is two and a half years old, right?
I do now .
@dean - ????????
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|