The path class and the directory class and the file class

Source: Internet
Author: User

Read Catalogue
    • Begin

Path action on a string of paths

Get suffix

Can merge paths

Get file name

Directory and DirectoryInfo operations on directories

Determine if a directory exists

Create a Directory

Delete Directory

Get all subdirectories under directory

Get all the sub files in the directory

File and FileInfo operations on files

Read the file

Write a file

Append file

Determine if a file exists

Create a file

deleting files

1. Path class

1 usingSystem;2 usingSystem.IO;//Namespaces for directory and file operations3 namespace_11_path Class {4     classProgram {5         Static voidMain (string[] args) {6             stringPath ="C:\\abc\\1.txt" ;7             //Note here is the operation on the path string instead of the real file "modify" support string level, no really renaming the file8Path = path.changeextension (path,"avi");//changeextension () modify the file suffix name 1.avi c:\\abc\\1.avi9             //combine two paths to a path, better than +, can be convenient to solve the problem is not a diagonal line, automatically handle the problem of path separatorsTenPath = Path.Combine ("c:\\abc\\def\\","1.jpg");//c:\abc\def\1.jpg One             //The location of the folder where the files are located is also handled from a string perspective APath = path.getdirectoryname (path);//C:\ABC -Path = path.getextension (path);//extension. txt -Path = path.getfilename (path);//file name. suffix name 1.txt thePath = path.getfilenamewithoutextension (path);//file name without suffix 1 -Path = Path.GetFullPath ("11-path class. exe");//file full path (relative to the full path of the file generally does not use this method) F:\PIZIYIMAO\11-Path class \bin\debug\11-path class. exe -Path = Path.gettempfilename ();//temporary folder save path automatically creates files C:\Documents and settings\piziyimao\local settings\temp\tmp5e.tmp -Path = Path.gettemppath ();//Get temporary folder save path C:\Documents and settings\piziyimao\local settings\temp\ + Console.WriteLine (path); - Console.read (); +         } A     } at}

2, Operation directory and DirectoryInfo

1 usingSystem;2 usingSystem.IO;3 namespace_12_directory {4     classProgram {5         Static voidMain (string[] args) {6DirectoryInfo dic =NewDirectoryInfo ("C:\\ABC" );7             //DiC. Name; //Get file name8             //DiC. FullName; //getting the file full path feature is more powerful than directory, except that it is an instance class and the latter is a static class9Directory. CreateDirectory ("C:\\ABC");//Create a folderTenDirectory. CreateDirectory ("c:\\abc\\1\\2\\3\\4\\5\\6\\7");//create multi-level folders consecutively One             if(Directory. Exists ("C:\\ABC"))//determine if a folder exists A             { -Directory. Delete ("C:\\ABC");//Delete if the folder is empty can be deleted if it is not empty then the error "Directory is not Empty" -Directory. Delete ("C:\\ABC",true);//true Specifies that if the folder is not empty, the same delete operation is performed the             } -             string[] paths = Directory. GetDirectories ("C:\\ABC");//get all subdirectory names in the directory note that you can use this method only if you remove a level that is c:\abc\1, such as getting all the folder paths under the Windows folder -             string[] Paths2 = Directory. GetDirectories ("C:\\Windows","$*");//the above method overload implementation retrieves the file starting with $ -             string[] PATHS3 = Directory. GetDirectories ("C:\\ABC","*", SearchOption. Alldirectories);//wildcard find eligible files in a folder include child folders +             foreach(stringPathinchpaths) { - Console. WriteLine (path); +             } A             string[] files = Directory. GetFiles ("C:\\Windows");//traverse all files under a folder at             string[] Files2 = Directory. GetFiles ("C:\\Windows","*.ini", SearchOption. Alldirectories);//Wildcard Lookup directory file usage is similar to GetDirectories -             foreach(stringFileinchfiles2) { - Console. WriteLine (file); -             } -             //The most important thing about directory operations is the GetFiles and GetDirectories methods -Directory. GetParent ("c:\\abc\\1\\2\\3\\4\\5\\6\\7");//returns the parent directory of the 7 folder C:\abc\1\2\3\4\5\6 in Console. Read (); -         } to     } +}

3. File type

usingSystem;usingSystem.IO;usingSystem.Text;namespace_13_file {classProgram {Static voidMain (string[] args) {            //File static classes need to be aware of the use of files when the default encoding is used if the encoding is incorrect in the file display garbledFile. Appendalltext ("C:\\1.txt","gb1232");//Append Content "gb2312" to the C:\\1.txt file//If there is a write file            if(File. Exists ("C:\\1.txt") ) {File. WriteAllText ("C:\\1.txt","writing in Chinese sometimes garbled requires using the third parameter to specify the encoding format of the encoding file, default format", Encoding. Default);//WriteAllText is completely covered and appendalltext is appended            }            //File.readalltext ();//reading a file no longer enumerates the following methods to view the document no longer an example//string[] ReadAllLines (string path)//reading a text file into an array of strings//string ReadAllText (string path)//reading a text file into a string//writealllines (String path,string[] contents),//saving a string array row-by-line to the file path overwrites the old content. FileInfo fi =NewFileInfo ("C:\\2.txt");//instantiated class functionality is more powerful than fileFi. AppendText ();//It has a lot of methods and properties to view the document yourselfConsole.        Read (); }    }}

Path class and directory class and file 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.