'Changing the default icon in a Windows Forms application

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?



Solution 1:[1]

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:[2]

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:[3]

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:[4]

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

Solution 5:[5]

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:[6]

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:[7]

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:[8]

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:[9]

Put the following line in the constructor of your Form:

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

Solution 10:[10]

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:[11]

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:[12]

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

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

Solution 13:[13]

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:[14]

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