C#反射:GetManifestResourceStream

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;


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值