Practical tips for C # file manipulation

Source: Internet
Author: User
Tags create directory flush readline c# copy file

//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"));

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.