请问如何在Visual Studio.net里使用后缀名为.resources的文件?如何将“image.resources”正确嵌入或链接到程序集“testresource”?
有一个名为image.resources的文件,里面有一些图标文件,例如new.ico save.ico cut.ico close.ico open.ico ...我知道用csc.exe编译可选
/res:image.resources 然后程序里可以用如下代码提取图标文件:
static ResourceManager rm;
rm = new ResourceManager("image",Assembly.GetExecutingAssembly());
this.label1.Image = ((Bitmap)rm.GetObject("new", null));
上面的情况是没有使用VS.NET的情况,可以通过编译,也可以执行。
在使用VS.NET时就会出现以下异常,但是可以通过编译,不能执行:
"未处理的“System.Resources.MissingManifestResourceException”类型的异常出现在 mscorlib.dll 中
其他信息:未能在给定的程序集中找到任何适合于指定的区域性(或非特定区域性)的资源。请确保已将“image.resources”正确嵌入或链接到程序集“testresource”。
baseName: image locationInfo: <null> resource file name: image.resources assembly: testresource, Version=1.0.0.3513, Culture=neutral, PublicKeyToken=null"
附:程序代码
Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Resources;
using System.Reflection;
using System.Data;
namespace testresource
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label label1;
static ResourceManager rm;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
rm = new ResourceManager("image", Assembly.GetExecutingAssembly());
//
// label1
//
//this.label1.Image = ((System.Drawing.Bitmap)(resources.GetObject("label1.Image")));
this.label1.Image = ((Bitmap)rm.GetObject("new", null));
this.label1.Location = new System.Drawing.Point(80, 56);
this.label1.Name = "label1";
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
项目testresource文件:
Form1.cs
image.resources
App.ico
AssemblyInfo.cs