C# console application icon
Asked Answered
L

3

29

Does anyone know how to set a C# console application's icon in the code (not using project properties in Visual Studio)?

Lot answered 23/7, 2009 at 8:18 Comment(0)
R
24

You can't specify an executable's icon in code - it's part of the binary file itself.

From the command line you'd use /win32icon:<file> if that's any help, but you can't specify it within the code of the application. Don't forget that most of the time the application's icon is displayed, your app isn't running at all!

That's assuming you mean the icon for the file itself in explorer. If you mean the icon of the application while it's running if you just double-click the file, I believe that will always just be the icon for the console itself.

Ruwenzori answered 23/7, 2009 at 8:20 Comment(3)
I'm not sure if that is going to work for me. I am compiling a concole application in a C# application, using CSharpCodeProvider and I really, really want to set up the explorer icon for the resulting binary...Lot
I found the compiler options: CompilerParameters cp = new CompilerParameters(); cp.CompilerOptions = "/optimize /target:winexe /win32icon:program.ico"; Thank you!Lot
Small side note: it seems the VS debugger occasionally starts console programs without properly showing their icon. But it doesn't affect the actual program; it's probably just because it gets wrapped in a debugger or something.Sudhir
P
31

You can change it in the project properties.

See this Stack Overflow article: Is it possible to change a console window's icon from .net?

To summarize right click on your project (not the solution) in Visual Studio and select properties. At the bottom of the "Application" tab there is a section for "Icon and manifest" where you can change the icon.

Plymouth answered 11/5, 2011 at 12:8 Comment(2)
Are we just going to ignore that OP specifically stated they want a way to do it in code...NOT from the project properties??Christianism
this provided the solution i was looking for. maybe not what the OP askedPontiff
R
24

You can't specify an executable's icon in code - it's part of the binary file itself.

From the command line you'd use /win32icon:<file> if that's any help, but you can't specify it within the code of the application. Don't forget that most of the time the application's icon is displayed, your app isn't running at all!

That's assuming you mean the icon for the file itself in explorer. If you mean the icon of the application while it's running if you just double-click the file, I believe that will always just be the icon for the console itself.

Ruwenzori answered 23/7, 2009 at 8:20 Comment(3)
I'm not sure if that is going to work for me. I am compiling a concole application in a C# application, using CSharpCodeProvider and I really, really want to set up the explorer icon for the resulting binary...Lot
I found the compiler options: CompilerParameters cp = new CompilerParameters(); cp.CompilerOptions = "/optimize /target:winexe /win32icon:program.ico"; Thank you!Lot
Small side note: it seems the VS debugger occasionally starts console programs without properly showing their icon. But it doesn't affect the actual program; it's probably just because it gets wrapped in a debugger or something.Sudhir
V
7

Here is a solution to change icon by code:

class IconChanger
{
    public static void SetConsoleIcon(string iconFilePath)
    {
        if (Environment.OSVersion.Platform == PlatformID.Win32NT)
        {
            if (!string.IsNullOrEmpty(iconFilePath))
            {
                System.Drawing.Icon icon = new System.Drawing.Icon(iconFilePath);
                SetWindowIcon(icon);
            }
        }
    }

    public enum WinMessages : uint
    {
        /// <summary>
        /// An application sends the WM_SETICON message to associate a new large or small icon with a window. 
        /// The system displays the large icon in the ALT+TAB dialog box, and the small icon in the window caption. 
        /// </summary>
        SETICON = 0x0080,
    }

    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, IntPtr lParam);


    private static void SetWindowIcon(System.Drawing.Icon icon)
    {
        IntPtr mwHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
        IntPtr result01 = SendMessage(mwHandle, (int)WinMessages.SETICON, 0, icon.Handle);
        IntPtr result02 = SendMessage(mwHandle, (int)WinMessages.SETICON, 1, icon.Handle);
    }// SetWindowIcon()
}
Venepuncture answered 24/1, 2020 at 13:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.