日期:2014-05-18  浏览次数:20847 次

指定为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/