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
- System.Reflection.Assembly appDll = System.Reflection.Assembly.GetExecutingAssembly();
- System.IO.Stream stream = appDll.GetManifestResourceStream("AccessConToolApp.Resources.Temp.mdb");
- byte[] fileByte = new byte[(int)stream.Length];
- stream.Read(fileByte, 0,(int)stream.Length);
- using (System.IO.FileStream fs = new System.IO.FileStream("d://tt.mdb", System.IO.FileMode.Create))
- {
- fs.Write(fileByte, 0, fileByte.Length);
- fs.Flush();
- }
- stream.Close();