请问如何在Visual Studio.net里使用后缀名为.resources的文件?如何将“image.resources”正确嵌入或链接到程序集“testresource”?

musictom 2002-07-23 12:58:51
有一个名为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

...全文
1020 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
BCLangRen 2002-09-26
  • 打赏
  • 举报
回复
在VS.NET中,您同样需要做类似的工作:
Project->Add Existing Item->All files->image.resources,这样,您的image.resources就以Embeded


加了这个就好了。我也遇到这样的问题 。搞了半天。
还有。
ResourceManager rm = new ResourceManager("image",Assembly.GetExecutingAssembly());
这里的image 要与资源文件名相同

还有我想问一下,如何在设计的时候直接编译,而不用通过,csc这样的步骤,好麻烦。

evergreen 2002-08-03
  • 打赏
  • 举报
回复
是不是忘了使用rw.Close()?
musictom 2002-07-25
  • 打赏
  • 举报
回复
老大,不行啊


老大,不行啊,我以前就是这样加进去的,你注意出错信息:未处理的“System.Resources.MissingManifestResourceException“
我看了别人的一个项目文件,里面多了个*.exe.manifestbak文件,是不是这个原因?不过这个文件应该是编译出来的啊,另外我的resources文件
是用程序编译出来的,等一下我把源文件贴出来,你帮忙分析分析。你使用*.resources文件是就是按照你的方法做的?没有出错吗?我这样做怎么不行。555
makeresource.cs

using System;
using System.Drawing;
using System.Resources;

class MakeResources {
public static void Main() {

ResourceWriter rw = new ResourceWriter("image.resources");
Bitmap a = new Bitmap("image/ABOUT.ico");//1
Bitmap b = new Bitmap("image/aligncenter.ico");//2
Bitmap c = new Bitmap("image/alignleft.ico");//3
Bitmap d = new Bitmap("image/alignright.ico");//4
Bitmap e = new Bitmap("image/attribute.ico");//5
Bitmap f = new Bitmap("image/cascade.ico");//6
Bitmap g = new Bitmap("image/centeralign.ico");//7
Bitmap h = new Bitmap("image/CLOCK.ico");//8
Bitmap i = new Bitmap("image/CLOSE.ico");//9
Bitmap j = new Bitmap("image/close1.ico");//10
Bitmap k = new Bitmap("image/CLOSEALL.ico");//11
Bitmap l = new Bitmap("image/COLOR.ico");//12
Bitmap m = new Bitmap("image/Copyoftitle.ico");//13
Bitmap n = new Bitmap("image/Copyof.ico");//14
Bitmap o = new Bitmap("image/copy.ico");//15
Bitmap p = new Bitmap("image/cut.ico");//16
Bitmap q = new Bitmap("image/delete.ico");//17
Bitmap r = new Bitmap("image/find.ico");//18
Bitmap s = new Bitmap("image/FIND1.ico");//19
Bitmap t = new Bitmap("image/FIND2.ico");//20
Bitmap u = new Bitmap("image/FIND3.ico");//21
Bitmap v = new Bitmap("image/findnext.ico");//22
Bitmap w = new Bitmap("image/findpre.ico");//23
Bitmap x = new Bitmap("image/focusnext.ico");//24
Bitmap y = new Bitmap("image/focuspre.ico");//25
Bitmap z = new Bitmap("image/font.ico");//26
Bitmap aa = new Bitmap("image/FONT1.ico");//27
Bitmap bb= new Bitmap("image/FONT2.ico");//28
Bitmap cc = new Bitmap("image/FONT3.ico");//29
Bitmap dd = new Bitmap("image/go.ico");//30
Bitmap ee = new Bitmap("image/hex.ico");//31
Bitmap ff = new Bitmap("image/new.ico");//32
Bitmap gg = new Bitmap("image/NOTEBOOK.ico");//33
Bitmap hh = new Bitmap("image/OPEN.ico");//34
Bitmap ii = new Bitmap("image/open1.ico");//35
Bitmap jj = new Bitmap("image/paste.ico");//36
Bitmap kk = new Bitmap("image/preview.ico");//37
Bitmap ll = new Bitmap("image/print.ico");//38
Bitmap mm = new Bitmap("image/printsetting.ico");//39
Bitmap nn = new Bitmap("image/redo.ico");//40
Bitmap oo = new Bitmap("image/replace.ico");//41
Bitmap pp = new Bitmap("image/save.ico");//42
Bitmap qq = new Bitmap("image/saveall.ico");//43
Bitmap rr = new Bitmap("image/tool.ico");//44
Bitmap ss = new Bitmap("image/TRUANS.ico");//45
Bitmap tt = new Bitmap("image/undo.ico");//46
Bitmap uu = new Bitmap("image/wordwrap.ico");//47

rw.AddResource("ABOUT", a);
rw.AddResource("aligncenter", b);
rw.AddResource("alignleft", c);
rw.AddResource("alignright", d);
rw.AddResource("attribute", e);
rw.AddResource("cascade", f);
rw.AddResource("centeralign", g);
rw.AddResource("CLOCK", h);
rw.AddResource("CLOSE", i);
rw.AddResource("close1", j);
rw.AddResource("CLOSEALL", k);
rw.AddResource("COLOR", l);
rw.AddResource("Copyoftitle", m);
rw.AddResource("Copyof", n);
rw.AddResource("copy", o);
rw.AddResource("cut", p);
rw.AddResource("delete", q);
rw.AddResource("find", r);
rw.AddResource("FIND1", s);
rw.AddResource("FIND2", t);
rw.AddResource("FIND3", u);
rw.AddResource("findnext", v);
rw.AddResource("findpre", w);
rw.AddResource("focusnext", x);
rw.AddResource("focuspre", y);
rw.AddResource("font", z);
rw.AddResource("FONT1", aa);
rw.AddResource("FONT2", bb);
rw.AddResource("FONT3", cc);
rw.AddResource("go", dd);
rw.AddResource("hex", ee);
rw.AddResource("new", ff);
rw.AddResource("NOTEBOOK", gg);
rw.AddResource("OPEN", hh);
rw.AddResource("open1", ii);
rw.AddResource("paste", jj);
rw.AddResource("preview", kk);
rw.AddResource("print", ll);
rw.AddResource("printsetting", mm);
rw.AddResource("redo", nn);
rw.AddResource("replace", oo);
rw.AddResource("save", pp);
rw.AddResource("saveall", qq);
rw.AddResource("tool", rr);
rw.AddResource("TRUANS", ss);
rw.AddResource("undo", tt);
rw.AddResource("wordwrap", uu);


rw.Generate();
}
}
freesoul_ms 2002-07-24
  • 打赏
  • 举报
回复
感谢您使用微软产品。

正如您得到的错误信息那样,您必须将“image.resources”资源文件正确嵌入或链接到您的程序集“testresource”。在命令行您需要选择:/res:image.resources,在VS.NET中,您同样需要做类似的工作:
Project->Add Existing Item->All files->image.resources,这样,您的image.resources就以Embeded

Resource形式被嵌入到程序中,在运行时才不会出错。

希望能对您有所帮助,感谢您关注和使用微软技术!

======================
- 微软全球技术中心

本贴子仅供CSDN的用户作为参考信息使用。其内容不具备任何法律保障。您需要考虑到并承担使用此信息可能带来的风险。具体事项可参见使用条
款(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调
查(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
======================

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧