我定义了一个方法,用于添加窗体的ICO图标
void createResource()
{
//我已经添加了一个updateIco.ico文件了,在属性中已是"嵌入资源程序";ResourceTest是我的项目名,ResourceFiles
//是我新建的文件夹名
            Icon ico=new Icon(this.GetType().Assembly.GetManifestResourceStream("ResourceTest.ResourceFiles.updateIco"));
this.Icon=ico;}
然后我在private void InitializeComponent()中加入了此方法

// 
// Form1
// 
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(368, 293);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
createResource();//在这里
this.ResumeLayout(false);

可是运行没有报错,但就是不能改变FORM1的图标呢??

解决方案 »

  1.   

    对于自定义文件夹内的资源貌似不能省扩展名的。
    另外,只是应用程序的图标,如果只有一个的话最好不要专门开文件夹,避免不必要的麻烦……好像资源文件找不到会返回 null,由于 Form 的 Icon 属性能够接受 null 值,当然不报错了。
      

  2.   

    zChen:
    我加了扩展名也是不行的!!
      

  3.   

    忘记所了,资源
    ----------------------
    Assembly.GetManifestResourceStream() 方法从此程序集加载指定清单资源,清单资源的范围由指定类型的命名空间确定。
    [C#]
    public virtual Stream GetManifestResourceStream(
       Type type,
       string name
    );参数
    type 
    其命名空间用于确定清单资源名的范围的类型。 
    name 
    请求的清单资源的名称。 
    返回值
    表示此清单资源的 Stream。异常
    异常类型 条件 
    ArgumentNullException name 参数为空引用(Visual Basic 中为 Nothing)。 
    ArgumentException name 参数是空字符串 ("")。 
    SecurityException 调用方没有所要求的权限。 备注
    此方法对公共和私有资源都起作用。示例
    如果 type 的全名为“MyNameSpace.MyClasses”,而且 name 为“Net”,则 GetManifestResourceStream 将搜索名为 MyNameSpace.Net 的资源。
    -------------------------------
    请注意你的命名空间。假设你的文件目录结构如下:
    解决方案文件夹
    |-项目文件夹
    |  |-ResourceFiles
    |  |  |-updateIco.ico
    ...|-你的项目文件命名空间结构如下:
    ResourceTest
    |-Form1那么你搜索的资源范围就是 
    ResorceTest.参数名也就是说,你的代码查找的资源实际上是:
    ResorceTest.ResorceTest.ResourceFile.updateIco.ico
    实际上你需要的是:
    ResorceTest.ResourceFile.updateIco.ico
    所以你的代码应该是:
    Icon ico=new Icon(this.GetType().Assembly.GetManifestResourceStream("ResourceFiles.updateIco.ico"));//注意删除了前面的 ResourceTest 可能可以解决问题。下面提供了一个查看程序集资源名的方法,加入窗体的 Load 事件最后一行,在“输出”窗口中查看,必要是自己添加命名空间:
            public static void ShowResList(){
                System.Reflection.Assembly thisExe; 
                thisExe = System.Reflection.Assembly.GetExecutingAssembly();
                string [] resources = thisExe.GetManifestResourceNames();
                string list = "";            // Build the string of resources.
                foreach (string resource in resources)
                    list += resource + "\r\n";
                System.Diagnostics.Debug.WriteLine(list);        }