天天看點

C# Winform 嵌入資源

http://blog.csdn.net/mygisforum/article/details/5356682

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。

1.擷取嵌入資源

在 Visual Studio開發環境中設定此編譯器選項

1.在“解決方案資料總管”中,建立檔案夾"Floder";

2.在"Floder"下添加檔案,選擇要嵌入的檔案。

3.右鍵檢視屬性,将“生成操作”設定為“嵌入的資源”。

擷取方法:

System.Reflection.Assembly appDll = System.Reflection.Assembly.GetExecutingAssembly();

System.IO.Streamstream= appDll.GetManifestResourceStream("Namespace.Folder.Filename");

注意:

System.Reflection.Assembly.GetExecutingAssembly(); //資源在目前應用程式中

System.Reflection.Assembly.GetCallingAssembly(); //資源在類庫中

示例代碼:

[csharp]  view plain  copy

  1. System.Reflection.Assembly appDll = System.Reflection.Assembly.GetExecutingAssembly();  
  2. System.IO.Stream stream = appDll.GetManifestResourceStream("AccessConToolApp.Resources.Temp.mdb");  
  3. byte[] fileByte = new byte[(int)stream.Length];  
  4. stream.Read(fileByte, 0,(int)stream.Length);  
  5. using (System.IO.FileStream fs = new System.IO.FileStream("d://tt.mdb", System.IO.FileMode.Create))  
  6. {  
  7.     fs.Write(fileByte, 0, fileByte.Length);  
  8.     fs.Flush();  
  9. }  
  10. stream.Close();  
C# Winform 嵌入資源