C # obtain the file name based on the file path and the file name string,
C # several methods for intercepting the name and extension of a file with a path, etc.
C # These are often used in disk I/O operations, such as paths, files, file names, and file extensions.
Previously, it was often implemented by cutting strings,
But it is often wrong, especially the starting position, how many bytes, often get dizzy.
The following method seems simple:
String filePath = @ "E: \ Randy0528 \ Chinese directory \ JustTest.rar ";
Response. Write ("file path:" + filePath );
Response. Write ("<br/> change the path string extension. <Br/> ");
Response. Write (System. IO. Path. ChangeExtension (filePath, "txt "));
Response. Write ("<br/> returns the directory information of the specified path string .. <Br/> ");
Response. Write (System. IO. Path. GetDirectoryName (filePath ));
Response. Write ("<br/> returns the extension of the specified path string. <Br/> ");
Response. Write (System. IO. Path. GetExtension (filePath ));
Response. Write ("<br/> returns the name and extension of the specified path string. <Br/> ");
Response. Write (System. IO. Path. GetFileName (filePath ));
Response. Write ("<br/> returns the file name of a specified path string that does not have an extension. <Br/> ");
Response. Write (System. IO. Path. GetFileNameWithoutExtension (filePath ));
Response. Write ("<br/> get the root directory information of the specified path. <Br/> ");
Response. Write (System. IO. Path. GetPathRoot (filePath ));
Response. Write ("<br/> returns the random folder name or file name. <Br/> ");
Response. Write (System. IO. Path. GetRandomFileName ());
Response. Write ("<br/> create a temporary file named zero byte on the disk and return the complete path of the file. <Br/> ");
Response. Write (System. IO. Path. GetTempFileName ());
Response. Write ("<br/> return the path of the temporary folder of the current system. <Br/> ");
Response. Write (System. IO. Path. GetTempPath ());
Response. Write ("<br/> determine whether the path includes the file extension. <Br/> ");
Response. Write (System. IO. Path. HasExtension (filePath ));
Response. Write ("<br/> gets a value indicating whether the specified path string contains absolute or relative path information. <Br/> ");
Response. Write (System. IO. Path. IsPathRooted (filePath ));
Execution result
File Path: E: \ Randy0528 \ Chinese directory \ JustTest.rar
Change the extension of the path string.
E: \ Randy0528 \ Chinese directory \ JustTest.txt
Returns the directory information of the specified path string ..
E: \ Randy0528 \ Chinese directory
Returns the extension of the specified path string.
. Rar
Returns the name and extension of the specified path string.
JustTest.rar
Returns the name of a specified path string without an extension.
JustTest
Obtains the root directory information of a specified path.
E :\
Returns the random folder name or file name.
Ct2h5b2h. sed
Create a temporary file named zero byte and return the complete path of the file.
C: \ Documents ents and Settings \ Randy \ Local Settings \ Temp \ tmpAD. tmp
Returns the path of the temporary folder of the current system.
C: \ Documents ents and Settings \ Randy \ Local Settings \ Temp \
Determine whether the path includes the file extension.
True
Gets a value indicating whether the specified path string contains absolute or relative path information.
True