1. For the path class, you can see from the compiler that this class is a static tool class. Note that this class is a string operation and has nothing to do with files.
1) changeextension () method, Modify the file suffix (call this method. If the second parameter is set to "", the extension is removed ).
1 // note that c: \ A \ BB \ CCC \ DDD \ 1.txt is not a real physical path 2 string Spath = path. changeextension (@ "C: \ A \ BB \ CCC \ DDD \ 1.txt ","*. xls "); 3 console. writeline (Spath );
The running result is as follows:
2) Combine () method, Merge paths.
1 string s1 = @"c:\a\b";2 string s2 = "c.doc";3 string fullPath = Path.Combine(s1, s2);4 Console.WriteLine(fullPath);
The running result is as follows:
3) obtain the directory of the path
1 string Path = @ "C: \ drivers \ win \ audio \ SaiI \ cxhdaudioapi. DLL "; 2 console. writeline (path. getdirectoryname (PATH); // obtain the directory section 3 console. writeline (path. getfilename (PATH); // obtain the file name 4 console. writeline (path. getfilenamewithoutextension (PATH); // obtain the file name (do not extend) 5 console. writeline (path. getextension (PATH); // obtain the file extension 6 console. readkey ();
The running result is as follows:
4) getfullpath)
1 string fullPath = Path.GetFullPath("1.txt");2 Console.WriteLine(fullPath);
The running result is as follows:
5) read files (readalltext, large files are not applicable)
1 string Msg = File.ReadAllText("1.txt");2 Console.WriteLine(Msg);
The running result is as follows:
6) Get the temporary directory
1 Console. writeline (path. gettemppath (); // return to the Temporary Folder path of the system.
A temporary directory is used by many software. For example, you can directly open a compressed package (rather than decompress the package) with the decompressed software, and the files you see will be placed in the temporary folder.
Gettempfilename () gets a unique temporary file name. This method creates a 0-byte file in the temporary directory.