Monthly Archives: September 2009

How to Check whether the application is running in IDE or as EXE

In our Windows Application, some times we may need to check whether our application is running in IDE or is running as EXE. A typical example would be, we might use Development Server while working in development/debug mode. Once the application is deployed we will be using Live Server. It is very hard if you keep on chaning the server names each time like while dev change the server to development and while live change it back to live. We might make mistakes while switching these servers. To avoid these difficulties we can use the following function to see whether our application is running in IDE or running as standalone EXE.

Module modMain
    Sub Main()
        If IsApplicationRunningInIDE() Then
            Console.WriteLine("I am working on IDE")
        Else
            Console.WriteLine("I am working on standalone EXE")
        End If
    End Sub
    Public Function IsApplicationRunningInIDE() As Boolean
        Return System.Diagnostics.Debugger.IsAttached()
    End Function
End Module

Project Type: Console
Language: VB.Net
Tested with: VS 2008

Incoming search terms:

How to properly terminate the application by passing the process name

We should not directly kill the process. We should properly close the main window of the application using CloseMainWindow().

Here is the proper way of terminating the application in VB.Net. The code below will request the application to close properly and will wait for 10 seconds. if the application is not closed in 10 seconds then it will kill the process.

As this is console application you cannot directly use the Application.DoEvents method. You need to add reference to System.Windows.Forms using Project > Add Reference and select System.Windows.Forms and hit OK.

Module modMain
  Sub Main()
    'How to call this function:
    'This will return true if the process is terminated; and false if unable to terminate the process
    Console.WriteLine(TerminateProcess("firefox")) 'This will terminate the firefox application
  End Sub
  Public Function TerminateProcess(ByVal sProcessName As String) As Boolean
    Dim lTimer As Long
    Try
      For Each oProc As Process In Process.GetProcessesByName(sProcessName)
        'To ask the process to exit.
        oProc.CloseMainWindow()
        lTimer = CLng(Microsoft.VisualBasic.Timer)

        Do Until oProc.HasExited
          System.Windows.Forms.Application.DoEvents()
          If Microsoft.VisualBasic.Timer - lTimer > 10 Then
            oProc.Kill()
          End If
        Loop
      Next oProc
      Return True
    Catch ex As Exception
      Console.WriteLine("Error 003: Unable to close the application " & sProcessName)
      Return False
    End Try
  End Function
End Module

Project Type: Console
Language: VB.Net
Tested with: VS 2008

Incoming search terms:

How to create a simple internet browser in Visual Basic 2008

Here is the presentation of how to create a simple web browsers using Visual Basic 2008:

Author: SimpleVbTuts

Incoming search terms:

How to get the active Window Title by passing the process name

Here is the VB.Net code which will help you to get the active Window title for the given process name:

Module modMain
  Sub Main()
    'How to call this function:
    Console.WriteLine(GetWindowTitleNameByProcessName("firefox"))
    'This will return the current title of firefox application
  End Sub
  Function GetWindowTitleNameByProcessName(ByVal sProcessName As String) As String
    Dim sResult As String = ""
    For Each oProc As Process In Process.GetProcessesByName(sProcessName)
      sResult = (oProc.MainWindowTitle)
    Next oProc
    Return sResult
  End Function
End Module

Tested with VS 2008

Incoming search terms:

How to get list of installed software names and its locations

The following VB.Net code grabs list of all installed software and its installed location from your PC.

Imports Microsoft.Win32
Module modMain
  Sub Main()
    Dim InstalledSoftwareKey As String = "SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products"
    Using RegKey As RegistryKey = Registry.LocalMachine.OpenSubKey(InstalledSoftwareKey)
      For Each iKey In RegKey.GetSubKeyNames()
        Dim KeyProperties As RegistryKey = Registry.LocalMachine.OpenSubKey(InstalledSoftwareKey, False).OpenSubKey(iKey, False).OpenSubKey("InstallProperties", False)
        Dim SoftwareName = KeyProperties.GetValue("DisplayName", "")
        Dim SoftwareInstalledPath = KeyProperties.GetValue("InstallLocation", "")
        Dim SoftwarePublisher = KeyProperties.GetValue("Publisher", "")
        Dim SoftwareUninstallString = KeyProperties.GetValue("UninstallString", "")
        Console.WriteLine(SoftwareName.ToString())
        Console.Write(" - ")
        Console.Write(SoftwareInstalledPath.ToString())
        'Console.Write(" - ")
        'Console.Write(SoftwarePublisher.ToString())
        'Console.Write(" - ")
        'Console.Write(SoftwareUninstallString.ToString())
        KeyProperties.Close()
      Next
    End Using
    Console.Read()
  End Sub
End Module

Tested with VS 2008

Incoming search terms: