How to set a WPF window's icon to an icon file through C#?

IneedHelp picture IneedHelp · Sep 12, 2012 · Viewed 9.4k times · Source

I have a WPF project with a window. If I want to set the window's icon property in C#, it allow me to feed it only an ImageSource and it doesn't accept icon files. If I set the property in XAML, it accepts icon files without any problem. How can I set the a WPF Window's icon to an icon file through C# code?

Answer

Nikhil Agrawal picture Nikhil Agrawal · Sep 12, 2012

How about

mywindow.Icon = new BitmapImage(new Uri(@"C:\myicon.ico"));