C#文件读写IO操作 实例

1.前台代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileTest.aspx.cs" Inherits="FileTest" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>文件IO操作</title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:FileUpload ID="FuReport" runat="server" />
        <br />
        <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
        <asp:Button ID="btnLoad" runat="server" OnClick="btnLoad_Click" Text="Load" />
        <asp:Button ID="btnCheck" runat="server" OnClick="btnCheck_Click" Text="Check" />
   
    </form>
</body>
</html>

 

2.后台代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class FileTest : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string path = "C://";
        Save(FuReport, path);
    }

    protected void btnLoad_Click(object sender, EventArgs e)
    {
        string path = "C://sql.txt";

        FileInfo Fi = new FileInfo(path);
        if (Fi.Exists)
        {
            StreamReader Sdr = new StreamReader(path, System.Text.Encoding.UTF8);
            string sLine = "";
            ArrayList arr = new ArrayList();
            while (sLine != null)
            {
                sLine = Sdr.ReadLine();
                if (sLine != null)
                {
                    arr.Add(sLine);

                }
            }
            Sdr.Close();
            foreach (string s in arr)
            {
                Response.Write(s + "<br />");
            }
        }
        else
            Response.Write("No file matched!!");
    }

    public bool Save(System.Web.UI.WebControls.FileUpload fuFile, string EndPath)
    {
        string FilePathName = "";
        try
        {
            FilePathName = System.IO.Path.GetFileName(fuFile.FileName);
            fuFile.PostedFile.SaveAs(EndPath + "//" + FilePathName);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return true;
    }

    public byte[] Load(string FilePath)
    {
        FileInfo fi = new FileInfo(FilePath);
        byte[] fileByte;
        try
        {
            FileStream fs = fi.OpenRead();
            fileByte = new byte[fs.Length];
            fs.Read(fileByte, 0, Convert.ToInt32(fs.Length));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return fileByte;
    }
    protected void btnCheck_Click(object sender, EventArgs e)
    {
        string path = "C://sql.txt";
        FileInfo Fi = new FileInfo(path);
        if (Fi.Exists)
        {
            StreamReader Sdr = new StreamReader(path, System.Text.Encoding.UTF8);
            string sLine = "";
            string A = "";
            string B = "";

            ArrayList arr = new ArrayList();
            while (sLine != null)
            {
                sLine = Sdr.ReadLine();
                if (sLine != null)
                {
                    arr.Add(sLine);

                    A=A+sLine.Replace("A","");
                    B = B + sLine.Replace("B", "");   
                }

            }
            Sdr.Close();

            WriteFile("C://A.txt", A);
            WriteFile("C://B.txt", B);

            foreach (string s in arr)
            {
                Response.Write(s + "<br />");
            }
        }
        else
            Response.Write("No file matched!!");
    }
    /// < ummary>
    /// 写文件
    /// </summary>
    /// < aram name="Path">文件路径</param>
    /// < aram name="Strings">文件内容</param>
    public static void WriteFile(string Path, string Strings)
    {
        if (!System.IO.File.Exists(Path))
        {
            System.IO.FileStream f = System.IO.File.Create(Path);
            f.Close();
        }
        System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.GetEncoding("gb2312"));
        f2.WriteLine(Strings);
        f2.Close();
        f2.Dispose();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值