Changing the default icon in a Windows Forms application

C#Visual StudioWinforms

C# Problem Overview


I need to change the icon in the application I am working on. But simply browsing for other icons from the project property tab -> Application -> Icon, it is not getting the icons stored on the desktop..

What is the right way of doing it?

C# Solutions


Solution 1 - C#

The icons you are seeing on desktop is not a icon file. They are either executable files .exe or shortcuts of any application .lnk. So can only set icon which have .ico extension.

> Go to Project Menu -> Your_Project_Name Properties -> > Application TAB -> Resources -> Icon

browse for your Icon, remember it must have .ico extension

You can make your icon in Visual Studio

> Go to Project Menu -> Add New Item -> > Icon File

Solution 2 - C#

Add your icon as a Resource (Project > yourprojectname Properties > Resources > Pick "Icons from dropdown > Add Resource (or choose Add Existing File from dropdown if you already have the .ico)

Then:

this.Icon = Properties.Resources.youriconname;

Solution 3 - C#

The Icon displayed in the Taskbar and Windowtitle is that of the main Form. By changing its Icon you also set the Icon shown in the Taskbar, when already included in your *.resx:

System.ComponentModel.ComponentResourceManager resources = 
    new System.ComponentModel.ComponentResourceManager(typeof(MyForm));
this.Icon = ((System.Drawing.Icon)(resources.GetObject("statusnormal.Icon")));

or, by directly reading from your Resources:

this.Icon = new Icon("Resources/statusnormal.ico");

If you cannot immediately find the code of the Form, search your whole project (CTRL+SHIFT+F) for the shown Window-Title (presuming that the text is static)

Solution 4 - C#

You can change the app icon under project properties. Individual form icons under form properties.

Solution 5 - C#

Once the icon is in a .ICO format in visual studio I use

//This uses the file u give it to make an icon. 

Icon icon = Icon.ExtractAssociatedIcon(String);//pulls icon from .ico and makes it then icon object.

//Assign icon to the icon property of the form

this.Icon = icon;

so in short

Icon icon = Icon.ExtractAssociatedIcon("FILE/Path");

this.Icon = icon; 

Works everytime.

Solution 6 - C#

I added the .ico file to my project, setting the Build Action to Embedded Resource. I specified the path to that file as the project's icon in the project settings, and then I used the code below in the form's constructor to share it. This way, I don't need to maintain a resources file anywhere with copies of the icon. All I need to do to update it is to replace the file.

var exe = System.Reflection.Assembly.GetExecutingAssembly();
var iconStream = exe.GetManifestResourceStream("Namespace.IconName.ico");
if (iconStream != null) Icon = new Icon(iconStream);

Solution 7 - C#

On the solution explorer, right click on the project title and select the 'Properties' on the context menu to open the 'Project Property' form. In the 'Application' tab, on the 'Resources' group box there is a entry field where you can select the icon file you want for your application.

Solution 8 - C#

I found that the easiest way is:

  1. Add an Icon file into your WinForms project.

  2. Change the icon files' build action into Embedded Resource

  3. In the Main Form Load function:

    Icon = LoadIcon("< the file name of that icon file >");

Solution 9 - C#

Put the following line in the constructor of your Form:

Icon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location);

Solution 10 - C#

The Simplest solution is here: If you are using Visual Studio, from the Solution Explorer, right click on your project file. Choose Properties. Select Icon and manifest then Browse your .ico file.

Solution 11 - C#

Select your project properties from Project Tab Then Application->Resource->Icon And Manifest->change the default icon

This works in Visual studio 2019 finely Note:Only files with .ico format can be added as icon

Solution 12 - C#

select Main form -> properties -> Windows style -> icon -> browse your ico

this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));

Solution 13 - C#

Try this, e.g. in the form constructor:

public Form1()
{
  InitializeComponent();

  string imagePath = @"" + AppDomain.CurrentDomain.BaseDirectory + "path-for-icon";
  using (var stream = File.OpenRead(imagePath))
  {
     this.Icon = new Icon(stream);
  }
}

Solution 14 - C#

This is the way to set the same icon to all forms without having to change one by one. This is what I coded in my applications.

FormUtils.SetDefaultIcon();

Here a full example ready to use.

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        //Here it is.
        FormUtils.SetDefaultIcon();

        Application.Run(new Form());
    }
}

Here is the class FormUtils:

using System.Drawing;
using System.Windows.Forms;

public static class FormUtils
{
    public static void SetDefaultIcon()
    {
        var icon = Icon.ExtractAssociatedIcon(EntryAssemblyInfo.ExecutablePath);
        typeof(Form)
            .GetField("defaultIcon", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static)
            .SetValue(null, icon);
    }
}

And here the class EntryAssemblyInfo. This is truncated for this example. It is my custom coded class taken from System.Winforms.Application.

using System.Security;
using System.Security.Permissions;
using System.Reflection;
using System.Diagnostics;

public static class EntryAssemblyInfo
{
    private static string _executablePath;

    public static string ExecutablePath
    {
        get
        {
            if (_executablePath == null)
            {
                PermissionSet permissionSets = new PermissionSet(PermissionState.None);
                permissionSets.AddPermission(new FileIOPermission(PermissionState.Unrestricted));
                permissionSets.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));
                permissionSets.Assert();

                string uriString = null;
                var entryAssembly = Assembly.GetEntryAssembly();

                if (entryAssembly == null)
                    uriString = Process.GetCurrentProcess().MainModule.FileName;
                else
                    uriString = entryAssembly.CodeBase;

                PermissionSet.RevertAssert();

                if (string.IsNullOrWhiteSpace(uriString))
                    throw new Exception("Can not Get EntryAssembly or Process MainModule FileName");
                else
                {
                    var uri = new Uri(uriString);
                    if (uri.IsFile)
                        _executablePath = string.Concat(uri.LocalPath, Uri.UnescapeDataString(uri.Fragment));
                    else
                        _executablePath = uri.ToString();
                }
            }

            return _executablePath;
        }
    }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSrivastavaView Question on Stackoverflow
Solution 1 - C#Javed AkramView Answer on Stackoverflow
Solution 2 - C#CsomotorView Answer on Stackoverflow
Solution 3 - C#Lorenz Lo SauerView Answer on Stackoverflow
Solution 4 - C#KristoferAView Answer on Stackoverflow
Solution 5 - C#JoshView Answer on Stackoverflow
Solution 6 - C#DovView Answer on Stackoverflow
Solution 7 - C#LEMUEL ADANEView Answer on Stackoverflow
Solution 8 - C#s kView Answer on Stackoverflow
Solution 9 - C#mrexodiaView Answer on Stackoverflow
Solution 10 - C#MeirDayanView Answer on Stackoverflow
Solution 11 - C#Chandhu KuttanView Answer on Stackoverflow
Solution 12 - C#srinivasanView Answer on Stackoverflow
Solution 13 - C#Sahan SiriwardanaView Answer on Stackoverflow
Solution 14 - C#Roberto BView Answer on Stackoverflow