C # performs an operation on an System.String instance that contains file or directory path information

Source: Internet
Author: User

C # performs an operation on an System.String instance that contains file or directory path information

There is a special kind of operation in a string operation that performs an operation on a System.String instance that contains file or directory path information. For example, a string representing a path gets its representative file name, folder path, file extension, and so on. In many cases, we like to use substring plus indexof to make string changes. The code is as follows:

stringFilePath =@ "C:\test\test2\test3.txt";//Get file information by substring plus indexof method stringFileName = filepath.substring (Filepath.lastindexof (' \ \ ') +1);//Get file information by substring plus indexof method stringFileextenstion = filepath.substring (Filepath.lastindexof ('. ') +1);//Get file information by substring plus indexof method stringFilenamewithoutextenstion = filepath.substring (Filepath.lastindexof (' \ \ ') +1, Filepath.lastindexof ('. ')-Filepath.lastindexof (' \ \ ') -1);

Above this practice the first two is relatively good to write, as long as the attention of the next +1 on the line, but the last one is not a bit can write right, I tuned two times before changing the right. And the readability of this code is very bad, without comment, the novice is very difficult to see clearly. Here is another way of writing, mainly using the FileInfo class.

VarFileInfo= new FileInfo (FilePath);stringName =FileInfo. Name;stringExtens =FileInfo. Extension;stringNamewithoutextenstion = name. Remove (name. IndexOf (".", stringcomparison.ordinal));

The code above looks better than the initial substring+indexof scheme, and is more reliable when it comes to writing, without worrying about forgetting "+1" errors like the one above. However, the following problems exist:

1. 2. 还是没法获取不带文件扩展名的文件名称;

In fact,. Net provides us with a better solution, which is the path class that we often use. The code to complete the above features is as follows:

string Namefrompath = path . getfilename  (FilePath); String extenstionfrompath = path . GetExtension (FilePath); String namewithoutextensionfrompath = path . Getfilenamewithoutextension (FilePath); 

The above code is very simple and easy to understand, and efficient, highly recommended. Here are some other APIs for path:

//Get directory of files stringDirName = Path.getdirectoryname (FilePath);//Get the full path of the file stringFullPath = Path.GetFullPath (FilePath);//Gets the root directory information for the specified path.  //NULL if path is null; //If path does not contain root directory information (such as a relative path), it is empty.  stringPathroot = Path.getpathroot (FilePath);//Get a random folder name or file name, save the name, but note that the name has a suffix.  stringRandomname = Path.getrandomfilename ();//Returns the path of the current user's Temp folder, ending with a backslash.  stringTempPath = Path.gettemppath ();//Get an array containing characters that are not allowed to be used in the file name, which can increase the validity of the input when writing the interface. Char[] Invalidfilename = Path.getinvalidfilenamechars ();//Gets an array that contains characters that are not allowed in the path name Char[] Invalidpathname = Path.getinvalidpathchars ();//combine a string array group into a path. (more useful than before, increase the support of uncertain parameters) stringCombinePath = Path.Combine (filePath1, FilePath2, filePath3);//Gets a value that indicates whether the specified path string contains the root BOOLIscontainsroot = path.ispathrooted (FilePath);

In addition to the above-mentioned methods, some other readonly variables are provided, which can be used when we write code. Specific as follows:

//Abstract: //provides platform-specific substitution characters that are used to separate the directory level in a path string that reflects the hierarchical file system organization.   Public Static ReadOnly CharAltdirectoryseparatorchar;// //Abstract: //provides platform-specific characters that are used to separate the directory level in a path string that reflects the hierarchical file system organization.   Public Static ReadOnly CharDirectorySeparatorChar;// //Abstract: A platform-specific delimiter used to separate path strings in environment variables.   Public Static ReadOnly CharPathSeparator;// //Abstract: //provides platform-specific volume separators.   Public Static ReadOnly CharVolumeseparatorchar;

C # performs an operation on an System.String instance that contains file or directory path information

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.