C # Fundamentals [11] File Management (Files Class)

Source: Internet
Author: User

Basic operations: Deposit, copy, move, delete
Basic methods:
File.exist ();
File.Copy ();
File.move ();
File.delete ()

Supplement: Text file encoding, text file has different storage way, the string in what form to save as binary, this is encoded, UTF-8, ASCII, UNICODE,GBK

GB2312, and so on, if garbled generally is the problem of coding, text file-related functions generally have a Encoding type of parameters, get the encoding method: Encoding.default, Encoding.UTF8, Encoding.GetEncoding ("GBK")
Output encoding.getencodings (), all encodings.
What is a text file. The text file that you can still read in Notepad is not doc.


Common static methods
void Appendalltext (string path, string contents),
Append text contents to the file path
BOOL Exists (string path) determines whether the file path exists
String[] ReadAllLines (string path) reads a text file into a string array
String ReadAllText (string path) reads a text file into a string
void WriteAllText (string path, string contents)
Saving text contents to the file path overwrites the old content.
Writealllines (String path,string[] contents),
Saves an array of strings to the file path in a row, overwriting the old content

Method of the File class

File.Copy ("source", "TargetFileName", true);//copy of file, true means "overwrite" when the file exists, and if not true, the file has an exception.
File.exists ();//Determine if the file exists
File.move ("source", "target");//Move (cut), think about how to rename a file?
File.delete ("path");//delete. What if the file does not exist? Not present, no error
File.create ("path");//Create File

Manipulating text files

File.ReadAllLines ("path", encoding.default);//Read all rows, return string[]
File.readalltext ("path", encoding.default);//Read all text return string
File.readallbytes ("path"),///Read file, return byte[], processing the file as binary.
===========================================
File.writealllines ("Path", new String[4], encoding.default);//writes a string array to the file by line.
File.writealltext ("Path", "string");//write all strings to the file
File.writeallbytes ("Path", new byte[5]);//write byte[] all to File
File.appendalltext ()//append string to File

usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_15file Class {classProgram {Static voidMain (string[] args) {            //Create a file//file.create (@ "C:\Users\SpringRain\Desktop\new.txt"); //Console.WriteLine ("Created successfully"); //Console.readkey (); //Delete a file//File.delete (@ "C:\Users\SpringRain\Desktop\new.txt"); //Console.WriteLine ("Delete succeeded"); //Console.readkey (); //1024byte=1kb//1024kb=1m//1024m=1g//1024g=1t//1024t=1pt//Copy a file//file.copy (@ "C:\Users\SpringRain\Desktop\code.txt", @ "C:\Users\SpringRain\Desktop\new.txt"); //Console.WriteLine ("Copy succeeded"); //Console.readkey (); //CutFile.move (@"C:\Users\SpringRain\Desktop\code.txt",@"C:\Users\SpringRain\Desktop\newnew.txt"); Console.WriteLine ("Cut Success");            Console.readkey (); //Console.WriteLine (sizeof (char)); //Console.readkey (); //Console.WriteLine (sizeof (string)); a "Dsfdsfds"//Console.readkey ();        }    }}

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.IO;namespace_16_ using the file class to manipulate the data {classProgram {Static voidMain (string[] args) {            //byte[] buffer = file.readallbytes (@ "C:\Users\SpringRain\Desktop\1.txt"); //encodinginfo[] en = encoding.getencodings (); //foreach (var item in en)//{            //Console.WriteLine (item.            DisplayName); //}            //Console.readkey (); //converts a byte array into a string//string s = Encoding.UTF8.GetString (buffer); //Console.WriteLine (s); //Console.WriteLine (buffer.            ToString ()); //encoding format: refers to how you store strings in the form of//A- z 0-9 Ascii 117 u----> Kanji--->gb2312 GBK//int n = (int) ' U '; //char c = (char) 188; //Console.WriteLine (c);            ////console.writeline (n);            //string s= "The weather is fine today, everywhere good scenery";            ////Convert a string into a byte array            //byte[] buffer= Encoding.Default.GetBytes (s);            ////Writes a text file to the computer in bytes            //file.writeallbytes (@ "C:\Users\SpringRain\Desktop\1.txt", buffer); //Console.WriteLine ("Write Success"); //Console.readkey (); //using the file class to implement a copy operation of a multimedia file//Read            byte[] buffer = File.readallbytes (@"C:\Users\SpringRain\Desktop\12333.wmv");            Console.readkey (); ////write            //file.writeallbytes (@ "C:\Users\SpringRain\Desktop\new.wav", buffer); //Console.WriteLine ("Copy succeeded"); //Console.readkey (); //byte[] Buffer=new byte[1024*1024*5]; //While (true)//{            //file.writeallbytes (@ "C:\Users\SpringRain\Desktop\123.wmv", buffer); //}        }    }}

C # Fundamentals [11] File Management (Files Class)

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.