C#中的文件操作(读取文件与写入文件)

 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 Enties;
using OAFrameWork;
using BusinessLogical;
using System.Text.RegularExpressions;
using System.Text;
using System.IO;
public partial class ascx_HtmlEdit : System.Web.UI.UserControl
{
    public string Inputbh;
    public string NewInputPath;//输入文件路径  
    public string InputTxtbt;      
    private string FilePath;
    private TextFile text;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            FilePath = "../"+Txtfilelink.Text;
           
            text = new TextFile(Server.MapPath(FilePath));
            if (FilePath != "")              
                webupload.Value = text.readFile();
        }
    }
    public string utfEncode(string temp)
    {
        //string gb2312info = temp;
        //string utfinfo = string.Empty;
        //Encoding utf8 = Encoding.UTF8;
        //Encoding gb2312 = Encoding.GetEncoding("unicode");

        Convert the string into a byte[].
        //byte[] unicodeBytes = gb2312.GetBytes(gb2312info);
        Perform the conversion from one encoding to the other.
        //byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);
        Convert the new byte[] into a char[] and then into a string.
        This is a slightly different approach to converting to illustrate
        the use of GetCharCount/GetChars.
        //char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
        //utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
        utfinfo = new string(asciiChars);
        //utfinfo = System.Text.Encoding.UTF8.GetString(asciiBytes);
        //return utfinfo;
        string strAnsi = temp;
        byte[] bytDatas = System.Text.Encoding.Default.GetBytes(strAnsi);
        string strUTF8 = System.Text.Encoding.GetEncoding("utf-8").GetString(bytDatas);
        return strUTF8;
    }
    protected void Conferm_Click(object sender, EventArgs e)
    {
            FilePath = "../" + Txtfilelink.Text;
            File.Delete(Server.MapPath(FilePath));//删除原文件
            FilePath = "../UpLoad/gg/" + Txtbt.Text + ".htm";
            text = new TextFile(Server.MapPath(FilePath));
            text.writeFile(webupload.Value.ToString().Replace("../../upload/gg/images/","images/"));//写入新文件
            //Conferm.PostBackUrl = this.Request.Url.ToString();
            Inputbh = Txtbh.Text;//文件编号
            InputTxtbt = Txtbt.Text;
            NewInputPath = "UpLoad/gg/" + InputTxtbt + ".htm";           
            string updatesql = "update File_detail set bt='" + InputTxtbt + "',filelink='" + NewInputPath + "'where bh='" + Inputbh + "'";
            try
            {
                ActiveCon.DataFactory.ExecuteCommand(updatesql);
            }
            catch (Exception ex)
            {
                throw ex;
            }                
            Response.Write("<script language=javascript>window.close();</script>");
        
    }
  
}

 

public class TextFile
{
    private string FILE_NAME;
    private string fileContent;
    private System.Text.Encoding enCode;
    public TextFile(String fileName)
    {
        FILE_NAME = fileName;
        enCode = System.Text.Encoding.Default;
    }
    public String readFile()//读取web文件到FCK
    {
        if (!File.Exists(FILE_NAME))
        {
            Console.WriteLine("{0} does not exist.", FILE_NAME);
           // CMsgBox.Show ("文件不存在");
            return null;
        }
        using (StreamReader sr = new StreamReader(FILE_NAME, enCode))
        {
            string input;
            while ((input = sr.ReadLine()) != null)
            {
                //input.Replace("gb2312", "utf-8");
                Console.WriteLine(input);
                fileContent += input;
            }        
            Console.WriteLine("The end of the stream has been reached.");
            sr.Close();
        }     
        return fileContent;
    }
    public void writeFile(string strContent)//写入文件
    {
        fileContent=strContent;
        if (!File.Exists(FILE_NAME))
        {
            Console.WriteLine("{0} does not exist.", FILE_NAME);

        }
        StreamWriter sw = new StreamWriter(FILE_NAME, false, enCode);
        {
            //String input;
            //while ((input = sw.ReadLine()) != null)
            //{
            //    //input.Replace("gb2312", "utf-8");
            //    Console.WriteLine(input);
            //    fileContent += input;
            //}
            //Console.WriteLine("The end of the stream has been reached.");
            sw.Write(fileContent);
            sw.Close();
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值