Results 1 to 7 of 7

Thread: Detect if the application is a console or WinForms application

  1. #1

    Thread Starter
    Fanatic Member Satal Keto's Avatar
    Join Date
    Dec 2005
    Location
    Me.Location
    Posts
    518

    Question 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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    New Member
    Join Date
    May 2013
    Posts
    1

    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

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    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

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Detect if the application is a console or WinForms application

    Quote Originally Posted by jmcilhinney View Post
    You guys do realise that this thread is two and a half years old, right?
    I do now .

    @dean - ????????
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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
  •  



Click Here to Expand Forum to Full Width