asdp.net 文件操作

来源:未知 责任编辑:智问网络 发表时间:2013-11-12 17:20 点击:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string filename = Server.MapPath("~/") + this.TextBox2.Text;
        FileStream fs = new FileStream(filename, FileMode.Create,
            FileAccess.Write, FileShare.None);
        StreamWriter sw = new StreamWriter(fs);
        sw.Write(this.TextBox1.Text);
        sw.Flush();
        sw.Close();
        fs.Close();
    }//创建文件
    protected void Button2_Click(object sender, EventArgs e)
    {
        string filename = Server.MapPath("~/") + this.TextBox2.Text;
        FileStream fs = new FileStream(filename, FileMode.Open,
            FileAccess.Read, FileShare.Read);
        StreamReader sr = new StreamReader(fs);
        this.TextBox1.Text = sr.ReadToEnd();
        sr.Close();
        fs.Close();
    }//读取文件
    protected void Button3_Click(object sender, EventArgs e)
    {
        string filename = Server.MapPath("~/") + this.TextBox2.Text;
        FileStream fs = new FileStream(filename, FileMode.Create,
            FileAccess.Write, FileShare.None);
        BinaryWriter bw = new BinaryWriter(fs);
        string[] data = this.TextBox1.Text.Split(new string[] { " " },
            StringSplitOptions.RemoveEmptyEntries);
        foreach (string str in data)
        {
            bw.Write(Convert.ToInt32(str));
        }
        bw.Close();
        fs.Close();
    }//创建二进制文件 www.2cto.com
    protected void Button4_Click(object sender, EventArgs e)
    {
        string filename = Server.MapPath("~/") + this.TextBox2.Text;
        FileStream fs = new FileStream(filename, FileMode.Open,
            FileAccess.Read, FileShare.None);
        BinaryReader br = new BinaryReader(fs);
        long nums = br.BaseStream.Length / 4;
        for (long i = 0; i < nums; i++)
        {
            this.TextBox1.Text += br.ReadInt32().ToString() + " ";
        }
        br.Close();
        fs.Close();
    }
}读取二进制文件

摘自 淡蓝蓝蓝

    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • 浅析.NET下XML数据访问新机制
    • asp.net 面试+笔试题目第1/2页
    • C# 邮件地址是否合法的验证
    • asp.net 设置GridView的选中行的实现代码
    • C#高级编程:数据库连接[1]
    • 经典C++程序1
    • IIS 自动回收导致后台定时器失效的问题解决
    • ASP.NET&#160;GridView列表代码示例
    • Asp.net MVC源码分析--Action Filter的链式调用
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1