How to minimize a window by passing the process name

The VB.Net code posted on this page minimizes the window for the given process name.

Module modMain
#Region "Declarations for Minimizing Windows"
  Private Declare Function ShowWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal nCmdShow As SHOW_WINDOW) As Boolean

  Private Enum SHOW_WINDOW As Integer
    SW_HIDE = 0
    SW_SHOWNORMAL = 1
    SW_NORMAL = 1
    SW_SHOWMINIMIZED = 2
    SW_SHOWMAXIMIZED = 3
    SW_MAXIMIZE = 3
    SW_SHOWNOACTIVATE = 4
    SW_SHOW = 5
    SW_MINIMIZE = 6
    SW_SHOWMINNOACTIVE = 7
    SW_SHOWNA = 8
    SW_RESTORE = 9
    SW_SHOWDEFAULT = 10
    SW_FORCEMINIMIZE = 11
    SW_MAX = 11
  End Enum
#End Region

  Sub Main()
    'How to call this function
    MinimizeWindows("acrobat") 'This will minimize the acrobat application
  End Sub

  Public Sub MinimizeWindows(ByVal sProcessName As String)
    Dim p As Process
    For Each p In Process.GetProcessesByName(sProcessName)
      ShowWindow(p.MainWindowHandle, SHOW_WINDOW.SW_MINIMIZE)
    Next p
  End Sub
End Module

Tested with VS 2008

Posted in VB.Net | Tagged c#.net .Net, How to, How to minimize a window, minimize window, VB.Net, vb.net code | Leave a comment

How to convert RGB Color to HTML color code

The following VB.Net code convert RGB color code to equivalent HTML color code.

Module modMain
  Sub Main()
    'How to call this function:
    Console.WriteLine(ConvertRGBToHTMLColor(255, 2, 2))
    Console.Read()
  End Sub

  Public Function ConvertRGBToHTMLColor(ByVal iRedColor As Integer, ByVal iGreenColor As Integer, ByVal iBlueColor As Integer) As String
    Dim sHexRed As String
    Dim sHexGreen As String
    Dim sHexBlue As String
    'Get Red hex code
    sHexRed = Int32.Parse(iRedColor).ToString("X")
    If sHexRed.Length < 2 Then sHexRed = "0" & sHexRed
    'Get Green hex code
    sHexGreen = Int32.Parse(iGreenColor).ToString("X")
    If sHexGreen.Length < 2 Then sHexGreen = "0" & sHexGreen
    'Get Blue hex code
    sHexBlue = Int32.Parse(iBlueColor).ToString("X")
    If sHexBlue.Length < 2 Then sHexBlue = "0" & sHexBlue
    Return "#" & sHexRed & sHexGreen & sHexBlue
  End Function
End Module

Tested with VS 2008

Posted in VB.Net | Tagged convert rgb to html, How to, How to convert RGB Color to HTML color code, html color code, rgb color code | Leave a comment

How to check for previous instances in .Net?

There are some instances you may want to disallow the user to launch your program if it is already running. There are few ways to check whether the previous instance of your application is already running. Here is one of the way to check whether previous instance of your application is already running or not:

VB.Net Code:

Imports System.Runtime.InteropServices
Module modMain
  Public Const ERROR_ALREADY_EXISTS As Integer = 183
  Public Declare Function CreateMutexA Lib "Kernel32.dll" (ByVal lpSecurityAttributes As Integer, ByVal bInitialOwner As Boolean, ByVal lpName As String) As Integer
  Public Declare Function GetLastError Lib "Kernel32.dll" () As Integer

  Public Function IsProcessAlreadyRunning() As Boolean
    'Attempt to create defualt mutex owned by process, MyApp can e.g. be the name of the executable/Product
    CreateMutexA(0, True, "MyApp")
    Return (GetLastError() = ERROR_ALREADY_EXISTS)
  End Function
End Module

C#.Net Code:

using System.Runtime.InteropServices;
static class modMain
{
    public const int ERROR_ALREADY_EXISTS = 183;
    [DllImport("Kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    public static extern int CreateMutexA(int lpSecurityAttributes, bool bInitialOwner, string lpName);
    [DllImport("Kernel32.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
    public static extern int GetLastError();

    public static bool IsProcessAlreadyRunning()
    {
        ///Attempt to create defualt mutex owned by process, MyApp can e.g. be the name of the executable/Product
        CreateMutexA(0, true, "MyApp");
        return (GetLastError() == ERROR_ALREADY_EXISTS);
    }
}

Another Simple way to accomplish this:

bool PrevInstance()
{
return Process.GetProcessesByName(Process.GetCurrentProcess.ProcessName).Length>1;
}

Tested with VS 2008

If you found any issues with the code posted above, please leave a comment. We will fix the code as soon as possible.

Posted in C#.Net, VB.Net | Tagged c#.net .Net, How to, How to check previous instance, previous instance, VB.Net, vb.net code | Leave a comment

How to check whether a file exists in FTP server

Here is the a VB.Net source code which will check whether a file is exists in FTP server.

Instructions:

  • Open a New Console application
  • Add a new bas module called Module1
  • Copy the following code to Mod1 bas module
Imports System.Net

Module Module1
  Sub Main()
    'How to call the IsFileExistsInFtpServer function:
    If IsFileExistsInFtpServer("ftp://ftp.mydomain.com/MyFile.doc") Then
      'File exists
      Console.WriteLine("File exists in FTP server.")
    Else
      Console.WriteLine("File doesn't exists in FTP server.")
    End If

  End Sub

  Public Function IsFileExistsInFtpServer(ByVal sFilePath As String) As Boolean
    Dim fwr As FtpWebRequest = WebRequest.Create(sFilePath)
    Dim fRes As FtpWebResponse
    fwr.Credentials = New NetworkCredential("FTP Username", "FTP Password")
    fwr.Method = WebRequestMethods.Ftp.GetFileSize
    Try
      fRes = fwr.GetResponse()
      'no error occured then the file is exists
      Return True
    Catch ex As WebException
      fRes = ex.Response
      'Error occured then the file doesn't exists
      If FtpStatusCode.ActionNotTakenFileUnavailable = fRes.StatusCode Then
        Return False
      Else
        'there might be other errors you need to handle here
        'like server might be down; in that case you cannot tell that
        'whether the file is exists or not
        Return True
      End If
    End Try
  End Function
End Module

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

Posted in VB.Net | Tagged check ftp file exists, file exists, ftp, IsFileExists | Leave a comment

How to Center a form in run time in .Net?

The built-in StartupPosition property will set the form to center of the screen when the form is loaded. Let’s say the user of your application moves the form and want it back to center of the screen again. The following piece of VB.Net code sets the form to center of the screen:

Private Sub CenterScreen(ByVal frm As Form)
  ' Get the Width and Height of the form
  Dim frm_width As Integer = frm.Width
  Dim frm_height As Integer = frm.Height

  'Get the Width and Height (resolution) of the screen
  Dim src As System.Windows.Forms.Screen = System.Windows.Forms.Screen.PrimaryScreen
  Dim src_height As Integer = src.Bounds.Height
  Dim src_width As Integer = src.Bounds.Width

  'Set the left and top property to move the form to center of the screen
  frm.Left = (src_width - frm_width) / 2
  frm.Top = (src_height - frm_height) / 2
End Sub

Tested with VS 2008

If you found any issues with the code posted above, please leave a comment. We will fix the code as soon as possible.

Posted in VB.Net | Tagged .Net, How to, How to Center a form, run time, vb.net code | Leave a comment