C#(042):System.IO.Path文件路径类(C#(042):System. IO. Path file path class)-c#
C#(042):System.IO.Path文件路径类(C#(042):System. IO. Path file path class)
Path类
以帮助在程序中管理文件和目录路径。
Path类位于System.IO命名空间,是一个静态类,可以用来操作路径的每一个字段,如驱动器盘符、目录名、文件名、文件扩展名和分隔符等。
Path类的静态属性和方法,此类操作不影响物理文件。
1、属性
Path类的常用字段成员有PathSeperator(路径分隔符,如”;”)、DirectorySeparatorChar(目录分隔符,如”\”)、VolumeSeparator(卷分隔符,如”:”)、AltDirectorySeparator(替换目录分隔符,如”/”),常用的方法成员有GetDirectoryName(取目录名)、GetFileName(取文件名)、
char a = System.IO.Path.VolumeSeparatorChar;// :
char b = System.IO.Path.DirectorySeparatorChar;// \
2、方法
GetExtension(取文件扩展名)、GetFullPath(取完整路径)、GetTempPath(取操作系统的临时文件路径)等,例如,以下代码表示提取并显示路径中的目录名和文件名。
string filePath =@”c:\folder\file.txt”;
Path.ChangeExtension(filePath, ".html");// c:\folder\file.htm
Path.Combine("c:\folder", "file.txt");// c:\folder\file.txt
Path.IsPathRooted(filePath); // true
Path.GetPathRoot(filePath); // C:\
Path.GetDirectoryName(filePath);// c:\folder
Path.GetFileName(filePath); // file.txt
Path.GetFileNameWithoutExtension(filePath);// file
Path.HasExtension(filePath); // true
Path.GetExtension(filePath); // .txt
Path.GetFullPath(filePath); // c:\folder\file.txt
Path类
To help manage file and directory paths in the program.
The path class is located in system IO namespace is a static class that can be used to operate every field of the path, such as drive letter, directory name, file name, file extension and separator.
Static properties and methods of the path class. Such operations do not affect physical files.
1. Attributes
The common field members of the path class are PathSeparator (path separator, such as “;”), directoryseparatorchar (directory separator, such as “” \ “), volumeseparator (volume separator, such as:” “), altdirectoryseparator (replace directory separator, such as” / “). The common method members are getdirectoryname (get directory name), getfilename (get file name)
char a = System.IO.Path.VolumeSeparatorChar;// :
char b = System.IO.Path.DirectorySeparatorChar;// \
2. Method
Getextension (take the file extension), getfullpath (take the full path), gettemppath (take the temporary file path of the operating system), etc. for example, the following code means to extract and display the directory name and file name in the path.
string filePath =@”c:\folder\file.txt”;
Path.ChangeExtension(filePath, ".html");// c:\folder\file.htm
Path.Combine("c:\folder", "file.txt");// c:\folder\file.txt
Path.IsPathRooted(filePath); // true
Path.GetPathRoot(filePath); // C:\
Path.GetDirectoryName(filePath);// c:\folder
Path.GetFileName(filePath); // file.txt
Path.GetFileNameWithoutExtension(filePath);// file
Path.HasExtension(filePath); // true
Path.GetExtension(filePath); // .txt
Path.GetFullPath(filePath); // c:\folder\file.txt