C#反射:GetManifestResourceStream

来源:互联网 发布:达瓦黑钢凛怎数据 编辑:程序博客网 时间:2024/06/07 04:39

public virtual Stream GetManifestResourceStream(string name)

参数

name
类型:System.String
所请求的清单资源的名称(区分大小写)。

 

name格式:项目名称 + 文件名

 

通过GetManifestResourceStream加载文件出现错误提示“null值”对于“stream”无效。

 

在做Mobile开发时,需要引入图片,用到了这个方法:

private Bitmap BackgroundImg = new Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(@"SmartDeviceProject1.bgmain.jpg"));

注:SmartDeviceProject1 :项目名称。bgmain.jpg :图片名称。

编译时候错误提示:“ null值”对于“stream”无效。 低级的错误让我郁闷了一天,最终解决办法如下:

1,把文件bgmain.jpg添加到项目中(在项目上右键—〉添加—〉添加现有项—〉找到并选择bgmain.jpg)

2,在已经添加到项目中的bgmain.jpg上右键—〉属性—〉生成的操作—〉选择“嵌入的资源”

3,正常运行

==============================================================================

现在就可以用这个图片给mobile的Form窗口绘制背景图片了:

private void Form1_Paint(object sender, PaintEventArgs e)
         {
             e.Graphics.DrawImage(BackgroundImg, 0, 0);

         }

 

 

using (Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("项目名称.文件名"))                {                    using (StreamReader sr = new StreamReader(stream))                    {                        _usageinfo = sr.ReadToEnd();                    }                }string version = System.Diagnostics.FileVersionInfo.GetVersionInfo(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName).ProductVersion;