using System;
using System.Windows.Forms;
using System.Drawing;
using System.Resources;
using System.IO;namespace ResourceTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class MakeResources
{ static void CreateResourceFile()
{
try
{ //add BMP resources
ResourceWriter rw = new ResourceWriter("ecalpon.resources");
Bitmap b = new Bitmap("fighter.bmp"); rw.AddResource("fighter", b); //add WAV resources
FileInfo fi = new FileInfo(Application.StartupPath + "\\hedead.wav");
byte[] wf = new byte[fi.Length]; FileStream fs = new FileStream(Application.StartupPath + "\\hedead.wav",
FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
br.BaseStream.Seek(0, SeekOrigin.Begin);
int bytecount = 0;
while(br.PeekChar() > -1)
wf[bytecount++] = br.ReadByte(); br.Close();
fs.Close();
fs = null; rw.AddResource("hedead", wf); rw.Generate();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//CreateResourceFile();
Application.Run(new fPlaySound());
}
}
}我很懒啊,没有试一试就发给你了。
using System.Windows.Forms;
using System.Drawing;
using System.Resources;
using System.IO;namespace ResourceTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class MakeResources
{ static void CreateResourceFile()
{
try
{ //add BMP resources
ResourceWriter rw = new ResourceWriter("ecalpon.resources");
Bitmap b = new Bitmap("fighter.bmp"); rw.AddResource("fighter", b); //add WAV resources
FileInfo fi = new FileInfo(Application.StartupPath + "\\hedead.wav");
byte[] wf = new byte[fi.Length]; FileStream fs = new FileStream(Application.StartupPath + "\\hedead.wav",
FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
br.BaseStream.Seek(0, SeekOrigin.Begin);
int bytecount = 0;
while(br.PeekChar() > -1)
wf[bytecount++] = br.ReadByte(); br.Close();
fs.Close();
fs = null; rw.AddResource("hedead", wf); rw.Generate();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//CreateResourceFile();
Application.Run(new fPlaySound());
}
}
}我很懒啊,没有试一试就发给你了。
包括读取那个bmp文件也是同样的异常,这是怎么回事啊?
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Resources;
using System.IO;namespace ResourceTest
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private System.IO.FileInfo fi;
private System.IO.FileStream fs;
private System.Resources.ResourceManager rm;
private System.Resources.ResourceWriter rw;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
Bitmap back=new Bitmap("Background.bmp"); rw=new ResourceWriter("My.resources");
rw.AddResource("Background.bmp",back); //add Mid resources
fi = new FileInfo("Main.mid");
byte[] wf = new byte[fi.Length]; fs = new FileStream("Main.mid",FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
br.BaseStream.Seek(0, SeekOrigin.Begin);
int bytecount = 0;
while(br.PeekChar() > -1)
wf[bytecount++] = br.ReadByte(); br.Close();
fs.Close();
fs = null; rw.AddResource("Main.mid", wf); rw.Generate();
rm=new ResourceManager("ResourceTest.My",this.GetType().Assembly);
this.BackgroundImage=(Image)rm.GetObject("Background.bmp"); //每次就在这里出错:未能在给定的程序集中找到任何适合于指定的区域性(或非特定区域性)的资源。请确保已将“ResourceTest.My.resources”正确嵌入或链接到程序集“ResoursceTest”。
baseName: ResourceTest.My locationInfo: <null> resource file name: ResourceTest.My.resources assembly: ResoursceTest, Version=1.0.1013.22360, Culture=neutral, PublicKeyToken=null
}
不过建议用rw.Close()代替rw.Generate();后面,改成这样:
rm=new ResourceManager("ResourceTest.My",Assembly.GetExecutingAssembly());确保你的资源文件名叫:My.resources
rm=ResourceManager.CreateFileBasedResourceManager("My",".",null);但是我想把resources文件嵌入exe(以后我的程序只有一个exe就好了),但是在项目里添加这个资源文件以后,在ResourceTest命名空间没有办法应用,在exe目录下如果没有这个资源文件还是不行,这怎么办啊?分不够再加!!
对了,你有没有把My.Resources加到工程里?然后属性里生成操作选嵌入的资源?
TagConv.resources放到工程目录。AssemblyInfo.cs里不设Culture:
[assembly: AssemblyCulture("")] 把TagConv.Resources加入工程,生成的操作:嵌入的资源调用:(Namespace 是TagConv)try
{
Assembly asm = Assembly.GetExecutingAssembly();
ResourceManager rm = new ResourceManager("TagConv.TagConv",asm);
object obj = rm.GetObject("BIG5");
_big5Table= new Byte[((Array)obj).Length];
Array.Copy((Array)obj,_big5Table,_big5Table.Length);
obj = rm.GetObject("GB");
_gbTable = new Byte[((Array)obj).Length];
Array.Copy((Array)obj,_gbTable,_gbTable.Length);
_inited=true;
}
catch(Exception ex)
{
Error.Log(ex.Message);
MessageBox.Show("无法加载资源");
_inited=false;
}没有任何问题!
ResourceManager rm = new ResourceManager("WindowsTmp.My",assembly);
this.BackgroundImage=(Image)rm.GetObject("Background.bmp");
我这个人很懒啊,不愿意多敲字,给你提供点资料,自己看看吧.创建完 .resources 文件后,请使用程序集链接器 (Al.exe) 将资源嵌入运行库二进制可执行文件或者将其编译为附属程序集。
ms-help://MS.VSCC/MS.MSDNVS.2052/cptools/html/cpgrfassemblygenerationutilityalexe.htm
你真晕啊,看不出来bugfree(八个非非),bugfree(八个飞飞之独立寒秋)都是一个bugfree.