0

I am struggling with how to change the main program icon during compilation. I know it can be changed in Visual Studio Project-> Properties -> Application -> and there is button Change icon.

Okay, how can I do exactly the same now without manually setting this icon? I just want to change it via the c# code. Is it possible at all? Screen settings looks like this: enter image description here

I tried this code in form load event , but it only changes icon after I start my application.

System.Drawing.Icon ico = new System.Drawing.Icon(FastFoodDemo.Properties.Resources.red_mark);
this.Icon = ico;

The question is : How can I change the application main icon during compilation without do it by hand like on the above screenshoot. Please don't make this post as duplicate, I searched a lot of webpages and couldn't find any solution in c#.

taktak
  • 90
  • 10
adam
  • 179
  • 2
  • 12
  • _"Please don't make this post as duplicate"_ - so if somebody had asked this exact question and received a perfectly adequate answer that also answers your question, you wouldn't want your question to be marked as a duplicate on principle? That seems weird. Anyway, why are you trying to do this? What is the end goal? Why is that better than manually editing this? Either way, I don't think you should expect to do this within your application code, since that isn't _executed_ during compilation. It should be part of your build process. – ProgrammingLlama Jul 30 '19 at 07:15
  • `this.Icon = ico;` changes the _form icon_. As you seem to be aware, the executable icon is separate. – ProgrammingLlama Jul 30 '19 at 07:18
  • Yeah i know it , so I write need to have the same result as changing by this button. – adam Jul 30 '19 at 07:19
  • 1
    Which problem you are trying to solve by changing icon "while compilation"? Why you can't use shown dialog? – Sinatr Jul 30 '19 at 07:20
  • Please answer the questions I asked. It's not really clear _why_ you are trying to do this. If you answered them, it would give everyone a better idea of what you're trying to achieve. – ProgrammingLlama Jul 30 '19 at 07:21
  • For example i need to compile 10 x my application with different exe icon. And need to load this icon while compilation without change it by this button after each build – adam Jul 30 '19 at 07:21
  • [What about a flag to MSBuild?](https://stackoverflow.com/a/17792708/3181933) – ProgrammingLlama Jul 30 '19 at 07:26
  • 1
    It's not clear how you run 10x compilations. I can't imagine the use for 10 identical software only having different icon. But you can surely replace `tool.ico` with new icon before build. – Sinatr Jul 30 '19 at 07:29
  • @Sinatr so how can i replace exacly this tool.ico? – adam Jul 30 '19 at 07:33
  • Did you look at the link I provided, Adam? You can pass a flag to MSBuild... – ProgrammingLlama Jul 30 '19 at 07:33
  • Yeah im watching it right now, but it gonna takes me a lot of times to understand msbuild.exe , maybe you can try help me and write cmd of that – adam Jul 30 '19 at 07:38
  • I think you just need `MSBuild MySolution.sln /target:publish /property:ApplicationIcon=companyA.ico /p:Configuration=Release`. (change Release/Debug as necessary). – ProgrammingLlama Jul 30 '19 at 07:39
  • Yeah, I just tested and that works (provided `companyA.ico` is in the project folder for the WinForms application). – ProgrammingLlama Jul 30 '19 at 07:41
  • So please post a answear of this question and make full path etc to works :) and gonna aprove it – adam Jul 30 '19 at 07:43

1 Answers1

2

You can use MSBuild to do this:

MSBuild MySolution.sln /target:publish /property:ApplicationIcon=companyA.ico /p:Configuration=Release

Note Configuration=Release - change this as needed.

Simply replace MySolution.sln with the name of your solution. The new icon should be within the project folder (assuming that is different to your solution folder).

  • The easy way to use MSBuild is to use the Developer Command Prompt (under Visual Studio 2017 in the Start menu).
  • The other way is to manually locate it under C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\bin - note that you might have to replace 2017, Community, and 15.0 depending on the version that you're using. See this question for alternatives.
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • ok , thats replace ico of the main application , but how can i do the same think and replace .ico in example : Form1.cs , is it possbile to do that with MSBuild.exe? – adam Jul 30 '19 at 09:13
  • Based on [this question](https://stackoverflow.com/questions/203456/how-can-i-get-the-icon-from-the-executable-file-only-having-an-instance-of-its), you could add `this.Icon = Icon.ExtractAssociatedIcon(System.Reflection.Assembly.GetExecutingAssembly().Location);` in the form's constructor. Otherwise, you need to set the icon from a resources file, and prepare it with the correct icon, I believe. – ProgrammingLlama Jul 30 '19 at 09:32