RE: Strange behavior when setting the icon of a window.

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



If anyone is interested, the behavior noted is because the
/resource:icon1.ico command line parameter to csc embeds the icon in the
executable program as icon1.ico but Visual Studio embeds the icon as
test2.icon1.ico. This is why the EndsWith() worked for csc and Visual Studio
but the use of test2.icon1.ico failed when compile by csc.

"hansolox1" wrote:

The following program simply sets the icon of a form called form1. When I
get the name of the embedded icon using GetManifestResourceNames(), I store
the name in a string variable called s. The value of s is "test2.icon1.ico".
This program will compile and run just fine from visual studio .net 2003
using .net 1.1 and also will compile and run just fine using csc form1.cs
/resource:icon1.ico.

Now, I created a new string called s2 and just simply stored the value
"test2.icon1.ico" in it. When I use s2 instead of s as a parameter to
GetManifestResourceSteam, the program will compile and run just fine in
Visual Studio 2003 but when compiled from the command line using cs form1.cs
/resource:icon1.ico, the GetManifestResourceString() method does not return a
valid string. This is really strange because the s and s2 strings are
identical.

Is this a bug or am I missing something?

Here is the code:


using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
using System.IO;
using System.Diagnostics;

namespace test2
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Form1_Load);
}

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
Stream imgStream = null;
Assembly a = Assembly.GetExecutingAssembly();
string [] resNames = a.GetManifestResourceNames();
foreach(string s in resNames)
{
if(s.EndsWith(".ico"))
{
string s2 = "test2.icon1.ico";
imgStream = a.GetManifestResourceStream(s2);

if( !(null==imgStream) )
{
System.Drawing.Icon ico = new Icon(imgStream);
this.Icon = ico ;
}
}
}
}
}
}

This code is stored in Form1.cs and added to a project called test2. It
also needs an embedded icon file called icon1.ico.

If this program is executed with:

imgStream = a.GetManifestResourceStream(s2);

The program will work from Visual Studio 2003 but not at a command line
using csc to compile.

If this program is executed with:

imgStream = a.GetManifestResourceStream(s);

The program will execute properly from Visual Studio 2003 and at a command
line using csc.

Thanks for the help.

.



Relevant Pages

  • Re: Cant find PInvoke DLL sqlceme30.dll (VS2005/SQLMobile)
    ... Visual Studio). ... Laxmi NRO, SQL Mobile, Microsoft Design Engineer ... Install CABs. ... > But when I execute the application on device I get "Can't find PInvoke DLL ...
    (microsoft.public.sqlserver.ce)
  • Re: Toplevel link-only project does not get linked during build
    ... note that T does NOT compile any source files. ... The problem is that when I execute "build solution", ... This happens identically in Visual Studio .net 2003 Professional Edition ...
    (microsoft.public.vc.ide_general)
  • demo dsw has nothing inside
    ... When I open the demo dsw with visual studio, ... so I cannot execute them. ... begin source code control ...
    (microsoft.public.vstudio.general)
  • Re: Problem Solution Explorer and forms
    ... The computer which runs Visual Studio belongs to a Windows 2003 domain. ... the icon issue still exists on this new machine. ... >cannot get the icon change to winform icon, ... Once you add the dependency dlls to the project and make it built ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Application Icon and Start Menu
    ... 256 Color icon for your app. ... Visual Studio or any third party icon editor. ... If you open the .inf file you will see this near the bottom ...
    (microsoft.public.dotnet.framework.compactframework)