C # File Operations

Source: Internet
Author: User
// C # Write/read the text file string fileName = @ "c:/111.txt ";
StreamReader sr = new StreamReader (fileName); string str = sr. readLine (); sr. close (); StreamWriter rw = File. createText (Server. mapPath (". ") +" // myText.txt ");
Rw. WriteLine ("write ");
Rw. WriteLine ("abc ");
Rw. WriteLine (". NET notes ");
Rw. Flush ();
Rw. Close (); // open a text file
StreamReader sr = File. OpenText (Server. MapPath (".") + "// myText.txt ");
StringBuilder output = newStringBuilder ();
String rl;
While (rl = sr. ReadLine ())! = Null)
{
Output. Append (rl + "");
} LblFile. Text = output. ToString ();
Sr. Close (); // C # append an object
StreamWriter sw = File. AppendText (Server. MapPath (".") + "// myText.txt ");
Sw. WriteLine ("Chasing ideals ");
Sw. WriteLine ("kzlll ");
Sw. WriteLine (". NET notes ");
Sw. Flush ();
Sw. Close (); // C # copy an object
String OrignFile, NewFile;
OrignFile = Server. MapPath (".") + "// myText.txt ";
NewFile = Server. MapPath (".") + "// myTextCopy.txt ";
File. Copy (OrignFile, NewFile, true); // C # delete an object
String delFile = Server. MapPath (".") + "// myTextCopy.txt ";
File. Delete (delFile); // C # Move a File
String OrignFile, NewFile;
OrignFile = Server. MapPath (".") + "// myText.txt ";
NewFile = Server. MapPath (".") + "// myTextCopy.txt ";
File. Move (OrignFile, NewFile); // C # create a directory
// Create the directory c:/sixAge
DirectoryInfod = Directory. CreateDirectory ("c: // sixAge ");
// D1 points to c:/sixAge/sixAge1
DirectoryInfod1 = d. CreateSubdirectory ("sixAge1 ");
// D2 points to c:/sixAge/sixAge1/sixAge1_1
DirectoryInfod2 = d1.CreateSubdirectory ("sixAge1_1 ");
// Set the current directory to c:/sixAge
Directory. SetCurrentDirectory ("c: // sixAge ");
// Create the directory c:/sixAge/sixAge2
Directory. CreateDirectory ("sixAge2 ");
// Create the directory c:/sixAge/sixAge2/sixAge2_1
Directory. CreateDirectory ("sixAge2 // sixAge2_1 ");

 

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.