Strange behavior when setting the icon of a window.

G

Guest

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.
 
G

Guest

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 said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top