Asp.Net文件处理

添加人:yeemio零级(113分)   添加时间:2007-09-01    阅读次数:2587  收藏此教程
FileStream:
FileStream 对于在文件系统上读取和写入文件非常有用, FileStream 缓存输入和输出,以获得更好的性能。
FileStream 类能够以同步或异步这两种模式之一打开文件,而且对同步方法(Read 和Write)和异步方法(BeginRead 和BeginWrite)有显著的性能影响。
在Windows系统中,如果输入输出数据小于64KB,则采用同步模式性能较好;而当大于64KB时,则最好采用异步模式。
FileSteam常用属性和方法:
• CanRead:判断当前流是否支持读取。
• CanWrite:判断当前流是否支持写入。
• CanSeek:是否支持搜索。
• IsAsync:是否处于异步打开模式。
• Postion:设置获取当前流所处位置。
• Flush:将当前缓存区的数据写入文件。
• Lock:锁定流,防止其他文件访问。
• Seek:设置当前流操作的指针位置。
还是来看例子来加深理解吧
第一个例子,FileStream来创建文件,页面就放一个Label就行了
后台编码:
 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Collections;
 5using System.Web;
 6using System.Web.Security;
 7using System.Web.UI;
 8using System.Web.UI.WebControls;
 9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO;
12using System.Text;
13namespace AspFile.FileStream
14{
15    public partial class FileStreamCreate : System.Web.UI.Page
16    {
17        protected void Page_Load(object sender, EventArgs e)
18        {
19            System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath(".") + "\FileStreamCreateText.txt", FileMode.Create, FileAccess.Write);
20            //建立StreamWriter为写做准备
21            StreamWriter rw = new StreamWriter(fs, Encoding.Default);
22            //使用WriteLine写入内容
23            rw.WriteLine("曾经有一份真挚的爱情放在我的面前。");
24            rw.WriteLine("而我没有珍惜,当我失去的时候,我才追悔莫及。");
25            rw.WriteLine("人世间最大的痛苦莫过于此,如果上天给我一个再来一次的机会。");
26            rw.WriteLine("我会对那个女孩说三个字:"我爱你。"");
27            rw.WriteLine("如果非要在这份爱上加一个期限的话,我希望是一万年。");
28            //将缓冲区的内容写入文件
29            rw.Flush();
30            //关闭rw对象
31            rw.Close();
32            fs.Close();
33            fs = new System.IO.FileStream(Server.MapPath(".") + "\FileStreamCreateText.txt", FileMode.Open, FileAccess.Read);
34            //打开文本文件
35            StreamReader sr = new StreamReader(fs, Encoding.Default);
36            StringBuilder output = new StringBuilder();
37            string rl;
38            while ((rl = sr.ReadLine()) != null)
39            {
40                output.Append(rl + "<br>");
41            }

42            lblFile.Text = output.ToString();
43            sr.Close();
44            fs.Close();
45        }

46    }

47}
第二个例子是读文件,注意这里就可以解决File下操作中的乱码问题,因为FileStream下边可以设置编码
放个选择文件的控件,一个按钮一个label。后台编码:
 1protected void Button1_Click(object sender, EventArgs e)
 2{
 3    string strFileStream;
 4    strFileStream = File1.PostedFile.FileName;
 5    if (Path.GetFileName(strFileStream) == "")
 6        return;
 7    System.IO.FileStream fs = new System.IO.FileStream(strFileStream,FileMode.Open,FileAccess.Read);
 8    //这里就可以设置编码,我们在这里指定的是系统默认的编码。
 9    StreamReader sr = new StreamReader(fs, Encoding.Default);
10    StringBuilder output = new StringBuilder();
11    string rl;
12    while ((rl = sr.ReadLine()) != null)
13    {
14        output.Append(rl + "<br>");
15    }

16    sr.Close();
17    fs.Close();
18    Label1.Text = output.ToString();
19}
第三个例子是可以进行流的复制文件。
添加html的inputfile控件,按钮,要复制到的路径,label。注意要复制到的地方必须具有写入操作的权限。
后台编码如下:
 1using System;
 2using System.Data;
 3using System.Configuration;
 4using System.Collections;
 5using System.Web;
 6using System.Web.Security;
 7using System.Web.UI;
 8using System.Web.UI.WebControls;
 9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11using System.IO;
12using System.Text;
13namespace AspFile.FileStream
14{
15    public partial class FileStreamCopy : System.Web.UI.Page
16    {
17        protected void Page_Load(object sender, EventArgs e)
18        {
19        }

20        protected void btnCopy_Click(object sender, EventArgs e)
21        {
22            string OriginFile = FileSelect.PostedFile.FileName;
23            string NewFile = tbDes.Text + "\" + Path.GetFileName(OriginFile);
24            //下面开始操作
25            //建立两个FileStream对象
26            System.IO.FileStream fsOF = new System.IO.FileStream(OriginFile, FileMode.Open, FileAccess.Read);
27            System.IO.FileStream fsNF = new System.IO.FileStream(NewFile, FileMode.Create, FileAccess.Write);
28            //建立分别建立一个读写类
29            BinaryReader br = new BinaryReader(fsOF);
30            BinaryWriter bw = new BinaryWriter(fsNF);
31            //将读取文件流指针指向流的头部
32            br.BaseStream.Seek(0, SeekOrigin.Begin);
33            //将写入文件流指针指向流的尾部
34            bw.BaseStream.Seek(0, SeekOrigin.End);
35            while (br.BaseStream.Position < br.BaseStream.Length)
36            {
37                //从br流中读取一个Byte并马上写入bw流
38                bw.Write(br.ReadByte());
39            }

40            br.Close();
41            bw.Close();
42            //操作后判断源文件是否存在
43            if (System.IO.File.Exists(NewFile))
44            {
45                lbInfo.Text = "附件复制成功!";
46            }

47            else
48            {
49                lbInfo.Text = "文件复制失败!";
50            }

51        }

52    }

53}
DirectoryInfo和FileInfo类:
• Directory(File) 类的所有方法都是静态的,因而无需具有目录的实例就可被调用。DirectoryInfo (FileInfo)类只包含实例方法。
• Directory (File)类的静态方法对所有方法执行安全检查。如果打算多次重用一个对象,请考虑改用DirectoryInfo(FileInfo)的相应实例方法,因为安全检查并不总是必要的。
这些例子都很简单,大家可以参照上边的例子自己做一下。然后最后还有个网络资源管理器的例子,这个篇幅太大了,这里我就不说明了。有想要最后网络管理器例子的朋友请给我留言,或者自己去MSDN下载。
2页 第2上一页12下一页
相关的教程: ASP.NET
收藏此教程

当前平均分: 0.0(0 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: