Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: (untagged)
I am using WPF NotifyIcon https://www.codeproject.com/Articles/36468/WPF-NotifyIcon-2 by Philipp, for one of my tools. I am unable to create a notify icon without admin rights in the system tray. The tool will run at end user's machines which will not have any admin rights and the tool is failing to create the notify icon.

What I have tried:

This is the code in my Main() method
C#
public static void Main(string[] args)
{
    DOTray dt = new DOTray();
    //Make the icon visible. Most imp
    MyDOIcon.Visibility = System.Windows.Visibility.Visible; 
}

This is the WPF window code
C#
<Window 
        x:Class="DeviceOne.DOTray"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="DeviceOne"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DeviceOne"
        xmlns:gif="http://wpfanimatedgif.codeplex.com"
        xmlns:app="clr-namespace:DeviceOne"
        xmlns:tb="http://www.hardcodet.net/taskbar"
        WindowStyle="None"
        WindowStartupLocation="CenterScreen"
        BorderBrush="Gray"
        BorderThickness="1"
        Icon="/Resources/DOBlue.ico"
        ShowInTaskbar="False"
        Visibility="Hidden"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        mc:Ignorable="d"
        Height="300" Width="300" Name="TopWindow" ResizeMode="NoResize">
    
    <Grid>
        <tb:TaskbarIcon x:Name="MyDOIcon" IconSource="Resources/DOBlue.ico" ToolTipText="DeviceOne"  Visibility="Collapsed" PopupActivation="LeftOrDoubleClick">
            <tb:TaskbarIcon.TrayToolTip>
                <Border
                    Background="#FF4B4B4B"
                    BorderBrush="#ffed00"
                    BorderThickness="2"
                    CornerRadius="4"
                    Opacity="0.8"
                    Width="120"
                    Height="40">
                    <TextBlock FontWeight="Bold" Foreground="#ffed00"
                        Text="DeviceOne"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center" />
                </Border>
            </tb:TaskbarIcon.TrayToolTip>
            <tb:TaskbarIcon.ContextMenu>

                <ContextMenu FontWeight="Bold" Foreground="#FF4B4B4B" FontStyle="Normal" FontFamily="Microsoft YaHei" BorderThickness="1.2" BorderBrush="#FF747474" >
                    
                    <MenuItem Header="User Info" ToolTip="Load User Information" Click="UserInfo_Click">
                        <MenuItem.Icon>
                            <Image Source="Resources/user-info-new.png"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                    
                    <MenuItem Header="Restore Windows" Click="RestoreWindows_Click" >
                        <MenuItem.Icon>
                            <Image Source="Resources/Restore-Window-Icon.png"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                    
                    <MenuItem Header="About DeviceOne" Click="AboutDeviceOne_Click" >
                        <MenuItem.Icon>
                            <Image Source="Resources/DO.png"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                    
                    <MenuItem Header="Exit" Click="ExitDeviceOne_Click" >
                        <MenuItem.Icon>
                            <Image Source="Resources/exit-icon.png"></Image>
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>
            </tb:TaskbarIcon.ContextMenu>
        </tb:TaskbarIcon>
        
    </Grid>
</Window>


This is failing to create icon when not run as admin. Are admin rights really needed to create a simple icon? or is something wrong with code? Please help!
Posted
Updated 7-Aug-19 5:29am
Comments
raddevus 7-Aug-19 15:42pm    
I know that there is no requirement under WinForms and using NotifyIcon so it is probably the same under WPF. Here's an example I just fired up and got running in LINQPad (Linqpad.net) and it displayed the notifyicon in the tray even though I'm not an admin on my machine : https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.notifyicon.-ctor?view=netframework-4.8#System_Windows_Forms_NotifyIcon__ctor
Mohith Kumar 14-Aug-19 7:47am    
This helped. Thanks!
raddevus 7-Aug-19 15:44pm    
Oh, also, I see that it looks as if WPF doesn't implement NotifyIcon according to this: https://stackoverflow.com/questions/17674761/can-i-use-notifyicon-in-wpf
But there is a project here on CP that may work you thru it: https://www.codeproject.com/Articles/36468/WPF-NotifyIcon
Richard Deeming 8-Aug-19 11:18am    
NB: The MyDOIcon variable in your Main method does not appear to have any relationship to the MyDOIcon control in your window.

I suspect you meant:
dt.MyDOIcon.Visibility = System.Windows.Visibility.Visible;

But if you're always setting that, it would be easier to set the property directly in the XAML markup instead.
Mohith Kumar 14-Aug-19 7:47am    
Yes, I am setting it the same way.

1 solution

Don't post this under Quick Answers - if you got the code from an article, then there is a "Add a Comment or Question" button at the bottom of that article, which causes an email to be sent to the author. They are then alerted that you wish to speak to them.
Posting this here relies on them "dropping by" and realising it is for them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900