C#中的文件操作(读取文件与写入文件)
来源:互联网 发布:汽车诊断软件笔记本 编辑:程序博客网 时间:2024/06/06 00:01
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();
}
}
}
- C#中的文件操作(读取文件与写入文件)
- C#中的文件操作(读取文件与写入文件)
- C#写入文件,与读取文件内容
- C# 读取写入文件
- php操作文件(读取写入文件)
- android 文件系统操作 文件读取与写入
- 文件的读取与写入操作
- 磁盘文件读取与写入操作
- 读取与写入文件
- C#文件写入操作
- C++文件操作——ini文件读取与写入
- c#文件读取和写入
- ini文件操作(读取/写入)
- 文件操作(读取和写入)
- C++文件读取 写入 操作
- C#操作Excel文件(读取Excel,写入Excel)
- C#操作Excel文件(读取Excel,写入Excel)
- C#操作Excel文件(读取Excel,写入Excel)
- 人工智能
- Windows下架设Subversion服务器
- ORACLE常用日期转换处理
- 开始学习svg
- 2008年05月24日---我的软件设计师考试
- C#中的文件操作(读取文件与写入文件)
- 修改My Eclipse的workspace
- LPSTR LPCSTR LPWSTR LPCWSTR PSTR的区别
- 十个习惯让你精通新的开发技术
- ASP.net页面的执行过程
- 纪念512地震遇难者
- asp技术问题
- 关于设置虚拟内存
- 如何将图片上传到远端文件夹并把路径写入数据库