任务:在C#app中使用资源... [英] Quest: Using Resource in C# app...

查看:48
本文介绍了任务:在C#app中使用资源...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个项目,我希望在运行时将一些文件作为资源添加到
访问(将它们复制到FS并使用)。


到目前为止,在VS.NET IDE中,我将文件添加到项目并将其Build Action设置为

嵌入式资源...


现在应该看看接下来......


任何帮助...

I am working on a project I want to add a few files as resource to
access(copy them to FS and use) at runtime.

So far in VS.NET IDE, I Add Files to the project and set its Build Action to
Embedded Resource...

Now where should look next...

Any help...

推荐答案

这有帮助吗?


public static Image RetrieveResourceImage(Assembly assembly,string

resourceName)

{

Image image = null;

尝试使用(Stream stream = RetrieveResourceStream(assembly,resourceName)){

if(stream!= null){

image = Image.FromStream(stream);

}


stream.Close();

} < br $>
}

catch(exception ex){

System.Diagnostics.Debug.WriteLine(string.Format(" RetrieveResourceImage({0})

Exception> {1}",resourceName,ex.Message) );

throw;

}


返回图片;

}


Frisky

" bj7lewis" < BJ ****** @ rio.com>在消息中写道

新闻:11 ************* @ corp.supernews.com ...
Does this help?

public static Image RetrieveResourceImage(Assembly assembly, string
resourceName)
{
Image image = null;
try {
using (Stream stream = RetrieveResourceStream(assembly, resourceName)) {
if (stream != null) {
image = Image.FromStream(stream);
}

stream.Close();
}
}
catch (Exception ex) {
System.Diagnostics.Debug.WriteLine(string.Format(" RetrieveResourceImage({0})
Exception> {1}", resourceName, ex.Message));
throw;
}

return image;
}

Frisky
"bj7lewis" <bj******@rio.com> wrote in message
news:11*************@corp.supernews.com...
我正在研究一个项目我想在运行时添加一些文件作为资源来访问(将它们复制到FS并使用)。

到目前为止,在VS.NET IDE中,我将文件添加到项目中并设置它的构建行动
到嵌入式资源......

现在应该在哪里看看...

任何帮助......
I am working on a project I want to add a few files as resource to
access(copy them to FS and use) at runtime.

So far in VS.NET IDE, I Add Files to the project and set its Build Action
to
Embedded Resource...

Now where should look next...

Any help...



> public static Image RetrieveResourceImage(Assembly assembly,string

[and]
> public static Image RetrieveResourceImage(Assembly assembly, string
[and]
using(Stream stream = RetrieveResourceStream(assembly,resourceName))
using (Stream stream = RetrieveResourceStream(assembly, resourceName))



{

这是什么?...


图片是绘图类或图像ctrl类,找不到
$ b MSDN上的$ b RetrieveResourceImage(...)或整个MSN INET搜索...


更多帮助...


{
What is this?...

Image is drawing class or Image ctrl class and can''t find
RetrieveResourceImage(...) on MSDN or whole MSN INET search...

Any more help...


Whoopsss .... :)


您可能也需要这个...


public static Stream RetrieveResourceStream(string resourceName)

{

返回RetrieveResourceStream(Assembly.GetEntryAssembly(),resourceName);

}


public static Stream RetrieveResourceStream(程序集程序,字符串

resourceName)

{

try {

返回程序集.GetManifestResourceStream(resourceName);

}

catch(Exception ex){

System.Diagnostics.Debug.WriteLine(string.Format(" RetrieveResourceStream({0})

异常> {1}",resourceName,ex.Message));

返回null;

}

}

基本上,您正在调用assembly.GetManifestResourceStream()。 (还有

其他版本。)


奖金:

大多数人遇到的最大问题是资源名称。 (我不认为
认为它记录得非常好。)为此,请使用此代码片段来重复
迭代程序集中所有资源的所有名称。


希望这有帮助...


public static void DebugIterateResources(Assembly assembly){

string [ ] resources = assembly.GetManifestResourceNames();

System.Diagnostics.Debug.WriteLine(" Assents for assembly:" +

assembly.GetName());

int index = 0;

foreach(资源中的字符串资源){

System.Diagnostics.Debug.WriteLine(" Resource" + index +":" +

资源);

index ++;

}

}

Frisky


" bj7lewis" < BJ ****** @ rio.com>在消息中写道

新闻:11 ************* @ corp.supernews.com ...
Whoopsss.... :)

You might need this too...

public static Stream RetrieveResourceStream(string resourceName)
{
return RetrieveResourceStream(Assembly.GetEntryAssembly() , resourceName);
}

public static Stream RetrieveResourceStream(Assembly assembly, string
resourceName)
{
try {
return assembly.GetManifestResourceStream(resourceName);
}
catch (Exception ex) {
System.Diagnostics.Debug.WriteLine(string.Format(" RetrieveResourceStream({0})
Exception> {1}", resourceName, ex.Message));
return null;
}
}

Basically, you are calling assembly.GetManifestResourceStream(). (There are
other versions.)

Bonus:
The biggest problem most people have is with the resource names. (I don''t
think it is documented very well.) For that, use this snippet of code to
iterate all the names of all the resources in your assembly.

Hope this helps...

public static void DebugIterateResources(Assembly assembly) {
string[] resources = assembly.GetManifestResourceNames();
System.Diagnostics.Debug.WriteLine("Resources for assembly: " +
assembly.GetName());
int index = 0;
foreach (string resource in resources) {
System.Diagnostics.Debug.WriteLine("Resource " + index + ": " +
resource);
index++;
}
}
Frisky

"bj7lewis" <bj******@rio.com> wrote in message
news:11*************@corp.supernews.com...
public static Image RetrieveResourceImage(Assembly assembly,string
public static Image RetrieveResourceImage(Assembly assembly, string


[and]


[and]

using(Stream stream = RetrieveResourceStream(assembly,
resourceName))
using (Stream stream = RetrieveResourceStream(assembly,
resourceName))


{
这是什么?...

图像是绘制类或图像ctrl类,在MSDN上找不到
RetrieveResourceImage(...)或整个MSN INET搜索...

更多帮助...


{
What is this?...

Image is drawing class or Image ctrl class and can''t find
RetrieveResourceImage(...) on MSDN or whole MSN INET search...

Any more help...



这篇关于任务:在C#app中使用资源...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆