//c# Write/read out text file
string fileName =@ "C:i.txt";
StreamReader sr = new StreamReader (fileName); String STR=SR. ReadLine (); Sr.close ();
Streamwriterrw=file.createtext (Server.MapPath (".") + "/mytext.txt");
RW. WriteLine ("write");
RW. WriteLine ("abc");
RW. WriteLine (". NET Notes ");
RW. Flush ();
RW. Close ();
//Open Text file
Streamreadersr=file.opentext (Server.MapPath (".") + "/mytext.txt");
Stringbuilderoutput=newstringbuilder ();
Stringrl;
while (RL=SR. ReadLine ())!=null)
... {
Output. Append (rl+ "");
}
Lblfile.text=output. ToString ();
Sr. Close ();
//c# Append file
Streamwritersw=file.appendtext (Server.MapPath (".") + "/mytext.txt");
SW. WriteLine ("Chasing the ideal");
SW. WriteLine ("kzlll");
SW. WriteLine (". NET Notes ");
SW. Flush ();
SW. Close ();
//c# Copy file
Stringorignfile,newfile
Orignfile=server.mappath (".") + "/mytext.txt";
Newfile=server.mappath (".") + "/mytextcopy.txt";
File.Copy (orignfile,newfile,true);
//c# Delete Files
StringdelfiLe=server.mappath (".") + "/mytextcopy.txt";
File.delete (delfile);
//c# Move file
Stringorignfile,newfile
Orignfile=server.mappath (".") + "/mytext.txt";
Newfile=server.mappath (".") + "/mytextcopy.txt";
File.move (Orignfile,newfile);
//c# Create directory
//www.bianceng.cn
//Create directory C:/sixage
directoryinfod=directory.createdirectory ("C:/sixage");
D1 point to C:/sixage/sixage1
Directoryinfod1=d.createsubdirectory ("SixAge1");
//d2 point to C:sixagesixage1sixage1_1
Directoryinfod2=d1. Createsubdirectory ("Sixage1_1");
//Set the current directory to C:/sixage
Directory.setcurrentdirectory ("C:/sixage");
//Create directory C:/sixage/sixage2
Directory.CreateDirectory ("SixAge2");
//Create directory C:sixagesixage2sixage2_1
directory.createdirectory ("Sixage2/sixage2_1");
However, in the operation of the TXT file read, seemingly no problem. Because the code can realize the read operation of files, but read TXT file contains Chinese when the garbled display. After half a day of data, seemingly complex problems in fact very simple can be solved, a little change can:
StreamReader sr = new StreamReader(fileName,Encoding.GetEncoding("gb2312"));