Dynamically Set Taskbar Icon for Windows Application - c#

My company has a branding mechanism that sets the icon for a Windows Application, like so:
if (AppBranding.Is("FOO"))
Icon = MyProject.Properties.Resources.Icon_FOO;
else
Icon = MyProject.Properties.Resources.Icon_BAR;
Icon_FOO and Icon_BAR are resources that point to their respective .ico files.
Also, Icon and manifest in Project > MyProject Properties > Resources is set in Visual Studio.
When users launch the application, the correct icon appears based on branding. However, if they pin the program to the taskbar, the icon reverts to Icon_FOO.ico, regardless of branding. When they unpin it, the icon toggles back.
I believe that programs pinned to the taskbar use the icon of the .exe, while the icon for the Window is whatever I set Form.Icon to be. I only know how to set the .exe icon by going to Project Properties > Resources and making a selection from the drop down list, as I have in the picture above. How can the icon for the .exe be set dynamically?

The icon in the taskbar is (usually) the first icon resource available in the assembly, and cannot be modified from within the application (unless you create a custom shortcut programmatically).
Instead of trying to manipulate this, you can create a simple starter application (*.exe) with the correct icon and version attributes for each branded version? The .exe than only needs to load the application from an assembly that contains the actual application content.
so you have
Launcher_FOO.exe
Launcher_BAR.exe
with their own custom icons. Both application will create a common application from
Actual_Application.dll
to start the application.
You may use Reflection.Emit to create the Launcher_xxx.exe on the fly:
see google search
see msdn

Related

Icon on Shortcut to .NET WinForms application automatically update

I have a VB.NET WinForms application which has a custom icon. I changed the icon (to something else) in the project's properties, and I can see it is correct on the EXE that is built, but all shortcut's to it retain the old icon. If I create a new shortcut the icon is correct.
When I try to set the icon on the shortcut manually (using Change Icon in the shortcut's properties) Windows report the error 'The file %1 contains no icons.', which I think may be part of the issue, as it isnt able to locate the icon automatically (so it doesn't update).
What can I do to get the icon on the shortcut to update automatically?
Update: Logging off and on again refreshes the icon cache, and the icons change. Also calling ie4uinit -ClearFontCache updates the icons, but this doesnt work in newer versions of Windows.
Try this.
http://blogs.technet.com/b/heyscriptingguy/archive/2005/08/12/how-can-i-change-the-icon-for-an-existing-shortcut.aspx
I'm not sure why you can't delete the old shortcuts and make new ones, but I hope this helps!

How to change launching icon of windows form application in microsoft visual c# 2010 express

When we start debugging the project, a form is available displaying the items. It has a default icon containing a square block. Can we change that icon??
In the Properties of the Form you have the property Icon. If you click on it you will get a little button where you can select a local file.
If you want to change the Icon of the compiled exe file you need to go to the properties of your project (right click on projrct in project explorer -> properties) and change the default icon.
goto form properties and choose icon select path of the images.....
form properties>icon>select>icon image.
Visual Studio 2010 Express C# has no icon editor,
If you want to use custom icons, you need a way to create them. The free GIMP graphics program will export images as .ico files, just use one layer per icon (say 16x16, 32x32, 48x48 & 64x64).
The .ico file can then be dragged and dropped onto the Project Properties tab Resources/Icons pane. CustomIcon.ico will be named as (and recognised by IntelliSense) as CustomIcon. This is built in at compile time and has no need to reference external files at run time.
It can be used to change the window title bar icon from the Form Code as follows:
Size size = new Size(16,16);
this.Icon = new Icon(YourProject.Properties.Resources.CustomIcon, size);
You could read this msdn article
http://msdn.microsoft.com/en-us/library/339stzf7.aspx
In the Form Code Side:
this.Icon = new Icon(#"C:\...");
In the Designer is in the Property-Window a property for the Form.
Right mouse click on form => Properties => Icon

Setting correct icon for Shortcut in VS2010 Setup Project

I have a small app with a setup project.
If I create a shortcut to my Primary Output, then this gets a standard Win7 icon and not the icon specified in my exe's Application Icon?
Whats the best way to make sure my desktop shortcut icon is the same icon as my exe?
e.g. If I change the exe's icon then I'd like the Setup icon to change as well.
If you set the icon for the shortcut you can set a primary output as source. In the file system editor of the setup project, click on the "(Icon)" combobox in the properties pane of the shortcut, then option "(Browse...)", button "Browse...". You'll see this:
Then add output and select the primary output of the exe.
You can add an icon from your target.exe file to the shortcut... in File System On Target Machine, browse to the shortcut in the folder you want to add the icon to (User's Desktop,etc). Right click the shortcut and select Properties Window. You'll see an Icon line... click it, select Browse, then browse to Application Folder/yourExe.exe (change from ICON to Exe files) and say OK. It will show you the icon from yourExe.exe that it has selected, say OK. You basically have to do the same thing for your Add/Remove Icon (appears in Control Panel/Uninstall)... properties page for your setup project. You have to do it for all shortcuts in different folders (User's Desktop, User's Startup, etc.) All icons good now!
To add to Gert's answer: make sure you build your project first. Otherwise the setup app will not see the icon.
Very simple way using IWshShortcut (works in .net 2 too):
WshRuntimeLibrary.IWshShortcut MyShortcut = (IWshRuntimeLibrary.IWshShortcut)WshShell.CreateShortcut(fullPathLnk);
string iconLoc = MyShortcut.IconLocation; // <- example: "c:\icon.ico,0"
I use to check if iconLoc still exists, fix if not, you can use to find and read icon file, etc etc

Application Icon doesn't change correctly using c#

I changed my Application's icon for a new one, by going to: "Project/MyProject Properties/Icon and Manifiest", and load the new icon. Now, in my debug folder the icon of my .exe file appear with the new icon, which is ok, but when I execute the .exe, the application icon in the taskbar still showing the old one.
Please advice.
You have two place to change your icon.
First place
The first place is in the project.
Right click on the project
Select Property
Go in Application Tabs
Choose Icon and Manifest and select the icon you want
Second place
The second place is in the property of your Winform.
Open the Form
Click on the Form
Press F4 or go in the property of the Form
Go down in the property to find "Icon"
Select the icon you want.
The reason you have a different icon in the taskbar than your application (.exe) icon is that the taskbar use the current form icon to display in the taskbar.
After encountering the same problem, I resolved it by doing the following:
Just stop your explorer.exe from task manager and rerun the explorer.exe again.
As a commenter mentioned, you should set in the properties of the *.ico file: Copy to Output Dir: Copy if newer.
This property is not absolutely required. I developed a winform application and tested it without icon. Then I created and added the icon. The icon showed when running with the VS debugger. I copied the bin/debug directory to another pc and there it ran with showing the icon.
But the icon did not show on the development machine when the app started by clicking the *.exe file.
Logout/login windows did not cure this.
Change the Copy To Output Dir property on the icon file to Copy If Newer, and rebuild the application, did help. Now I can start the app by clicking the *.exe and the icon shows nicely.
Conclusion:
It is not always required to build the app with the icon file copied to the output directory, but with this measure you will increase your chances.
Make sure that your *.ico file contains an icon of the proper size (like 16x16 for small task bars).
Copy your new icon in Project Properties --> resources --> icons
In your Main_Load function add:
this.Icon = Properties.Resources.newIcon;
Check this out for icon information and sizes it supports. Assuming this is just a simple error that you are getting check if your ico's are as per what is specified here http://msdn.microsoft.com/en-us/library/ms997636.aspx
I had the same problem and none of the above solved it.
In my case, I had defined the icon different for two different langages (default language english and german). You can see this if there appear two resources files: FormX.resx and FormX.de.resx
With the accepted answer only default icon was changed. But when running the application on my pc the german icon was used.
So I had to change the icon for both resources. In Visual Studio you can change the current resource language by switching the language item (in the forms properties) from default to another language.
I had the same problem. The "first place" mentioned by Patrick is about the file icon, i.e. the .exe aspect. The "second place" is about the form (in the upper left corner).
Restarting windows file explorer seemed to be a satisfactory solution too. But all this didn't work today. I didn't restart the computer, by the way.
This is what really displayed the new icon in the task bar: I realized that there was an old shortcut of the .exe on the desktop. Deleting the shortcut did the job.

Taskbar icon will not change once binary is installed

I have a WPF application in .net 3.5 sp1 where I change the main window icon periodically. I am seeing the taskbar icon change when running under the vs hosting process, when running directly from the bin/Debug or bin/Release folders, but not when the binary is installed and running from \Program Files! The titlebar icon of the window still changes correctly...
I'm setting the icon like this:
Uri imageSource = new Uri("pack://application:,,,/MyProject;component/Images/test.ico");
this.Icon = BitmapFrame.Create(imageSource);
Why isn't the taskbar icon changing?
I do have an ApplicationIcon set. This is what the taskbar icon is displaying instead of changing to what I set it to. According to msdn The ApplicationIcon is just a fallback in case this.Icon is not set.
I do not have the taskbar icon pinned.
The main window is not the startup window of the application. In fact, there is no startup window. The window is opened from a system tray icon.
The icons for the ApplicationIcon and test.ico mentioned above both have their compile action set to Resource.
The WiX installer is not doing anything special to the exe or icons. The exe is referenced like this:
<File Id="MyExe" Name="$(var.MyProject.TargetFileName)"
Source="$(var.MyProject.TargetPath)" KeyPath="yes" Vital="yes" />
I'm currently looking at the AppUserModelID to see if this can solve this but it seems unnecessary to override Windows 7's default behavior in this manner. I have two shortcuts created by WiX. One in the start menu's programs folder and one in the start menu's startup folder.
Update 18Aug11:
Ok this is just odd, odd, odd.
I started trying to solve another annoyance, that my window was allowing taskbar pinning when I don't really want it to (this is default win7 taskbar glomming). So I set IsHostApp REG_BINARY 0000 manually via regedit under HKCR\Applications\MyEXEName.exe. To have the change take effect I had to update the shortcuts that reference the app (or I could have reinstalled). Lo and behold, next time I start up the app, not only does the pinning feature disappear, but the taskbar icon properly updates!!!
Can someone explain how this has solved the issue?
When Windows Installer creates shortcuts in the Start menu, it sets the icon of the shortcuts which point to Installer cache of icons. This feature is required for advertising, where the referenced file does not exist but still the correct icon should be shown. It also allows showing the correct icon to the application if its main .exe is removed (and you can run MSI repair to fix it).
Your application is started with shortcut from Startup folder, and the icon of the shortcut is used on the Taskbar. (And you have the main window: no matter how you show it, when app starts or only when you click app icon in the notification area; moreover you have to have a window to be able to get clicks from notification area icon.)
When you disabled pinning your app to the Taskbar, it ignores the icon set in the shortcut that started your application, and therefore icon on Taskbar updates when you change the window icon.

Categories

Resources