随笔 - 317, 文章 - 0, 评论 - 453, 阅读 - 114万
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

c# 嵌入资源文件

Posted on   PHP-张工  阅读(16108)  评论(2编辑  收藏  举报

开发环境:VS2005 C#

首先将要嵌入的资源拷贝到工程目录下。

设置文件生成操作为:嵌入的资源

获取嵌入资源代码如下:

1
2
3
4
5
6
7
8
9
10
private void Form1_Load(object sender, EventArgs e)
{
    Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsApplication3.嵌入文本.txt");
    byte[] bs = new byte[sm.Length];
    sm.Read(bs, 0, (int)sm.Length);
    sm.Close();
    UTF8Encoding con = new UTF8Encoding();
    string str = con.GetString(bs);
    MessageBox.Show(str);
}

注意:WindowsApplication3.嵌入文本.txt WindowsApplication3为工程名称

实例下载:https://files.cnblogs.com/zjfree/Embed.rar

 

使用系统嵌入资源功能

假设工程名称为:WindowsApplication3

打开菜单 [项目] - [属性] 选择[资源] [添加资源] [添加现有文件] 选择资源文件 设置资源名称


在程序中使用方法

1
2
SoundPlayer play = new SoundPlayer(WindowsApplication3.Properties.Resources.Warning);
play.Play();
点击右上角即可分享
微信分享提示