File manipulation and encoding formats

The relevant class file operations
namespace is located in the System.IO
File action file, the overall operation of the file, copy, delete, cut, etc.
Directory operation directory (folder) static class
Path path to the file or directory operation ( operation is the string itself)
stream file stream
    1.FileStream memory stream file stream MemoryStream NetworkStream network flow
    2.StreamReader fast read text files
    3.StreamWriter fast write text file
    
path class common method
    string ChangeExtension (string path, string extension ) modify the file extension, only modify the level of the string, and rename the file without the real
    string Combine (string path1, string path2 )
    the two paths merge into one path, you can easily solve the problem without the slash, the automatic processing path delimiter the question as Combine String (@ "C: \ the TEMP", "a.jpg");
    String GetDirectoryName (String path)
    to get the file path name (folder)
    String GetExtension (String path)
    to get the file extension
    string GetFileName (string path)
    to get the file path of the file name part
    string GetFileNameWithoutExtension (string path)
    expanded by removing the name of the file name
    string GetFullFath (string path)
    to get the full path to the file, you can get the absolute path of the relative path
    
of the File class
    File.Create (string path)
    to specify the path to create a file
    File.Delete (string path)
    Deletes the specified path to the file
    File.Copy (string sourceFile, string destFile)
    copy the file
    File.Move (string sourceFile, string destFile)
    cut files
    
    using the file class read data
    1.File.ReadAllBytes (String path);    
    Console.WriteLine ( Encoding.Default.GetString (File.ReadAllBytes (@ "D: \ 1.txt")));
    bytes read the file type returns a byte array
    2.File.ReadAllLines (string path);
    all documents read returns a string array row
    String [] = File.ReadAllLines STR (path, Encoding.Default);
            the foreach (var TSTR in STR) {
                Console.WriteLine (TSTR);
            }
    3.File.ReadAllText (String path)
    to read the file all lines, a return string
    Console.WriteLine (File.ReadAllText (path, Encoding.Default) );
    
    use the file class write data
    1.File.WriteAllBytes (string path, byte [] buffer);
    writing the file in bytes type
    File.WriteAllBytes (@ "D: \ text.txt", Encoding.Default.GetBytes ( " self-discipline really so hard"));
    2.File.WriteAllLines (String path, String [] str)
    all lines written to the file
    File.WriteAllLines (@ "D: \ text.txt", new string [] { "asdf", "sdfa"});
    string array each element in one row
    3.File.WriteAllText (string paht, string content)
    All lines written to the file
    File.WriteAllText (@ "D: \ text.txt ", " Now the poor is only temporary, can not be poor forever");

The Directory class
    Directory class is a static class, all methods in this class are static, you need to call the class name
    commonly used method
    Directory.CreateDirectory (string path)
    specify the directory to create a folder
    Directory.Delete (string path, true)
    delete parameter specifies the directory ture is confirmed when an existing file in the specified directory delete
    Directory.Move (string path)
    cut specified directory
    
    string [] Directory.GetFileName (string path)
    get all the files in the specified directory and returns a string []
    to obtain the specified file in the specified directory add the parameter "* .mp3"
    
coding
    the string how to store binary data in the form of common format ASCII, GBK, close 2312, UTF-8

 

Are their own summary section above, may be summed up in some places is not very accurate, there are omissions, after slowly supplement

Guess you like

Origin www.cnblogs.com/bqjb9323/p/11784463.html
Recommended