468,510 Members | 1,733 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,510 developers. It's quick & easy.

Strange behavior when setting the icon of a window.

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.

Feb 28 '06 #1
1 1950
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.

Feb 28 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Grant Edwards | last post: by
4 posts views Thread by solex | last post: by
4 posts views Thread by ljlevend | last post: by
5 posts views Thread by michael sorens | last post: by
Plater
1 post views Thread by Plater | last post: by
reply views Thread by Lie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.