歡迎轉載,轉載請注明:轉載自[ 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/ ]