C # obtain the file name based on the file path and the file name string,

Source: Internet
Author: User

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

Related Article

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.