您的位置:首页 > 编程语言 > C#

C#对文件的操作 中文乱码

2012-07-29 00:07 183 查看
using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Collections;

using System.IO;

using System.Text;

public partial class _Default : System.Web.UI.Page

{

protected void page_load(object serder, EventArgs e)

{

//C#写入/读出文本文件

//解决中文乱码:1.把文本文件另存为utf-8编码 2.StreamReader sr = new StreamReader(fileName, Encoding.GetEncoding("gb2312"));

string fileName = @"c:/1.txt";

//StreamReader sr = new StreamReader(fileName);

StreamReader sr = new StreamReader(fileName, Encoding.GetEncoding("gb2312"));

string str = sr.ReadLine();

labContent1.Text = str.ToString();

sr.Close();

//StreamWriter rw = File.CreateText(Server.MapPath(".") + "/myText.txt");

//rw.WriteLine("写入");

//rw.WriteLine("abc");

//rw.WriteLine(".NET笔记");

//rw.Flush();

//rw.Close();

//打开文本文件

//StreamReader sr = File.OpenText(fileName);

////StreamReader sr = File.OpenText(Server.MapPath(".") + "/myText.txt");

//StringBuilder output = new StringBuilder();

//string rl;

//while ((rl = sr.ReadLine()) != null)

//{

// output.Append(rl + "");

//}

//labContent1.Text = output.ToString();

//sr.Close();

//C#追加文件

//StreamWriter sw=File.AppendText(Server.MapPath(".")+"/myText.txt");

//sw.WriteLine("追逐理想");

//sw.WriteLine("anyuan");

//sw.WriteLine(".NET笔记");

//sw.Flush();

//sw.Close();

//C#拷贝文件

//string OrignFile,NewFile;

//OrignFile=Server.MapPath(".")+"/myText.txt";

//NewFile=Server.MapPath(".")+"/myTextCopy.txt";

//File.Copy(OrignFile,NewFile,true);

//C#删除文件

//string delFile = Server.MapPath(".") + "/myTextCopy.txt";

//File.Delete(delFile);

//C#移动文件 ?可以理解为文件重命名

//string OrignFile,NewFile;

//OrignFile=Server.MapPath(".")+"/myText.txt";

//NewFile=Server.MapPath(".")+"/myTextCopy.txt";

//File.Move(OrignFile,NewFile);

//C#创建目录

//创建目录d:sixAge

//DirectoryInfo d = Directory.CreateDirectory("d:/sixAge");

//d1为d子目录 c:sixAge/sixAge1

//DirectoryInfo d1 = d.CreateSubdirectory("sixAge1");

//d2指向c:sixAge/sixAge1/sixAge1_1

//DirectoryInfo d2 = d1.CreateSubdirectory("sixAge1_1");

//将当前目录设为d:sixAge

//Directory.SetCurrentDirectory("d:/sixAge");

//创建目录d:sixAge/sixAge2

//Directory.CreateDirectory("sixAge2");

//创建目录d:sixAge/sixAge2/sixAge2_1

//Directory.CreateDirectory("sixAge2/sixAge2_1");

}

}

转自:http://hi.baidu.com/anyongqiang/item/487d6ec380749856bcef6975
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: