配色: 字号:
通过流的方式获取记事本或者Word里面的内容
2013-05-06 | 阅:  转:  |  分享 
  
一:需要引用的命名空间:
usingSystem.IO;
usingSystem.Text;
///
///获取文本文件的内容
///

protectedvoidGetTxt()
{
//--1.实例化一个文件流对象
FileStreamfile=newFileStream(@”F:\yz\VSproject\文档\test.txt”,FileMode.Open);
//--此处是文档的路径
if(file.CanRead)
{
byte[]b=newbyte[file.Length];
//--考虑到如果要显示的内容很大的话,则采取每次只读取1024个字节
int_intStart=0;
//--刚开始读取的位置
int_intRemain=Convert.ToInt32(file.Length);
//--文件流剩余的长度
int_intLength=1024;
//--每次读取的长度
while(_intRemain>=0)
{
int_intCurPos=0;
if(_intRemain<_intLength)
{
_intCurPos=file.Read(b,_intStart,Convert.ToInt32(_intRemain));
//--如果剩余文件流长度的小于1024则直接读取剩余的内容
}
else
{
_intCurPos=file.Read(b,_intStart,_intLength);
//--如果剩余文件流长度的大于1024,则每次读取1024个字符
}
//--改变初始值
_intStart+=_intCurPos;
_intRemain-=_intLength;
}
file.Flush();
file.Close();
Encodingen=Encoding.GetEncoding("GB2312");
ltlShowInfo.Text="读取的文件内容为:"+en.GetString(b);
}
}

///
///获取Word内容
///

protectedvoidGetWrod()
{
//以Office中的word形式打开,或者是下载
FileInfofile=newFileInfo(@”F:\yz\VSproject\文档\test.doc”);
//--此处要输入word文档的路径
Response.Clear();
Response.ClearHeaders();
Response.Buffer=false;
Response.ContentType="application/octet-stream";
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(file.FullName,Encoding.GetEncoding("GB2312")));
Response.AppendHeader("Content-Length",file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush();
Response.End();
}
献花(0)
+1
(本文系Honey_Dry首藏)