首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

指定为Embedded Resource的资源文件的操作有关问题

2012-01-02 
指定为Embedded Resource的资源文件的操作问题现在有一个Excel文件,通过指定为Embedded Resource,编译进ex

指定为Embedded Resource的资源文件的操作问题
现在有一个Excel文件,通过指定为Embedded Resource,编译进exe文件了。

窗体上有一个按钮,点击改按钮,需要将该Excel文件保存到磁盘上。

请问如何操作?



[解决办法]
//this code requires System.Reflection namespace

//get names of resources in the assembly
string[] myassembly
= Assembly.GetExecutingAssembly().GetManifestResourceNames();

//create stream from the resource. 
Stream theResource 
= Assembly.GetExecutingAssembly().GetManifestResourceStream(myassembly[0]);

 
//Create binary reader from the stream

BinaryReader br = new BinaryReader(theResource);
//then filestream
FileStream fs = new FileStream(Environment.GetEnvironmentVariable("TEMP") +
+"\\test.xls" , FileMode.Create); 
BinaryWriter bw = new BinaryWriter(fs); //and then binary writer
byte[] bt = new byte[theResource.Length]; //read the resource
theResource.Read(bt,0, bt.Length); //and then write to the file
bw.Write(bt); //don't forget to close all streams
br.Close();
bw.Close();
  
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) 

http://feiyun0112.cnblogs.com/

热点排行