c# 嵌入資源文件


歡迎轉載,轉載請注明:轉載自[ http://www.cnblogs.com/zjfree/ ]

開發環境: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);
}
漢字亂碼
Stream sm = Assembly.GetExecutingAssembly().GetManifestResourceStream("嵌入資源.會澤代碼.txt");

            byte[] bs = new byte[sm.Length];

            sm.Read(bs, 0, (int)sm.Length);

            sm.Close();

            string str = Encoding.GetEncoding("GB2312").GetString(bs);        ;

            MessageBox.Show(str);

 字符串: MessageBox.Show(嵌入資源.Properties.Resources.mystr);

注意:WindowsApplication3.嵌入文本.txt WindowsApplication3為工程名稱

實例下載:http://files.cnblogs.com/zjfree/Embed.rar

 

使用系統嵌入資源功能

假設工程名稱為:WindowsApplication3

打開菜單 [項目] - [屬性] 選擇[資源] [添加資源] [添加現有文件] 選擇資源文件 設置資源名稱


在程序中使用方法

1
2
SoundPlayer play = new  SoundPlayer(WindowsApplication3.Properties.Resources.Warning);
play.Play();

歡迎轉載,轉載請注明:轉載自[ http://www.cnblogs.com/zjfree/ ]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM