当前位置:编程学习 > asp >>

asdp.net 文件操作

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.zzzyk.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();
    }
}读取二进制文件

摘自 淡蓝蓝蓝

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,