Jan 312014
 
 January 31, 2014  Posted by at 11:04 am C# & F#, PowerShell  Add comments

I’m working on a Pluralsight course on continuous integration and to demonstrate the process in the introduction module I made a make believe CI system in PowerShell, When a build step failed I wanted notifications, and tray notifications can be quite handy (emails as well of course).

trayNotificationPowershell

 

2014-01-31 17-50-05

This is how you create a tray notification in PowerShell or/and in C#. Remember to add a reference to System.Windows.Forms.

2014-01-31 17-48-35

2014-01-31 17-55-27

I’m using the PowerShell for Visual Studio extension to write (syntax highlighting, intellisense, debugging etc.) and execute PowerShell scripts in Visual Studio.

You can of course do the same things in Windows PowerShell ISE

PowerShell:

function Show-Balloon-Warning{
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)

    $tray = New-Object System.Windows.Forms.NotifyIcon
    $tray.Icon = “C:\Users\IrisDaniela\Downloads\p.ico”
    $tray.BalloonTipIcon = “Error”
    $tray.BalloonTipText = “Iris you broke the build!!”
    $tray.BalloonTipTitle = “Build failed”
    $tray.Visible = $True
    $tray.ShowBalloonTip(10000)
}

C#

– I’ll have to come back and add syntax highlighting to this later- I’m on the tram Open-mouthed smile

using System.Drawing;
using System.Windows.Forms;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var tray = new NotifyIcon
            {
                Icon = new Icon(@”C:\Users\IrisDaniela\Downloads\p.ico”),
                BalloonTipIcon = ToolTipIcon.Error,
                BalloonTipText = “Iris your broke the build!”,
                BalloonTipTitle = “Build Failed”,
                Visible = true
            };

            tray.ShowBalloonTip(10000);
        }
    }
}

  3 Responses to “Tray notifications in C# and PowerShell”

  1. I use a similar approach, but I use Growl notifications instead. Granted, this means that you have to have Growl installed, rather than built in functionality, but I really like the notifications you can get.

    Gary

  2. This seems to be a popular topic.
    Since NotifyIcon implements IDisposable you have to call Dispose when you are done with the NotifyIcon. If not, you have a memory/resource leak.

  3. i want to make notification for my leave application project like facebook in asp.net with C# can u help me

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

What is 13 + 3 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)