Iris Classon
Iris Classon - In Love with Code

Tray notifications in C# and PowerShell

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);
        }
    }
}

Comments

Leave a comment below, or by email.
Gary Ewan Park
1/31/2014 2:32:00 PM
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 
Lars Gregersen
2/6/2014 2:48:19 AM
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. 
Deepak
4/27/2014 1:11:56 AM
i want to make notification for my leave application project like facebook in asp.net with C# can u help me 


Last modified on 2014-01-31

comments powered by Disqus