Get file full path, directory, extension, file name in C # path

Source: Internet
Author: User
Tags mscorlib

Common functions need to refer to the static method that System.IO directly can call path

 1 class Program 2 {3 static void Main (string[] args) 4 {5 6//Get the current running program's Directory 7 string filedir = Environment.currentdirectory; 8 Console.WriteLine ("Current program directory:" +filedir); 9 10//A file directory one string filePath = "C:\\jiyf\\benxh\\benxhcms.xml"; Conso Le.   WriteLine ("Directory of the File:" +filepath); string str = "Get the full path of the file:" + Path.GetFullPath (FilePath); -->c:\jiyf\benxh\benxhcms.xml15 Console.WriteLine (str); + str = "Get the directory where the file is located:" + path.getdirect Oryname (FilePath); -->C:\JIYF\BENXH17 Console.WriteLine (str); str = "The name of the get file contains a suffix:" + path.getfilename (FilePath  ); -->benxhcms.xml19 Console.WriteLine (str); str = "Get file name without suffix:" + path.getfilenamewithoutext Ension (FilePath); -->BENXHCMS21 Console.WriteLine (str); str = "Get path suffix extension name:" + path.getextension (FilePath); -->.xml23            Console.WriteLine (str); str = "Get the root of the path:" + path.getpathroot (FilePath); -->c:25 Console.WriteLine (str); Console.readkey (); 27 28}29}

program runs on the desktop

Path Class Introduction

  1 #region Assembly mscorlib.dll, v4.0.0.0 2//C:\Program Files (x86) \reference Assemblies\microsoft\framework\.  Netframework\v4.0\mscorlib.dll 3 #endregion 4 5 using System;  6 using System.Runtime.InteropServices;  7 using System.Security;  8 using System.Text; 9 namespace System.IO 11 {12//Abstract: 13//Perform actions on System.String instances that contain file or directory path information. These operations are performed in a cross-platform manner. [ComVisible (true)] public static class Path 16 {17//Abstract: 18//provide platform-specific replacement character, the replacement word trailing characters To separate directory levels in the path string that reflects the hierarchical file system organization. public static readonly char Altdirectoryseparatorchar; 20//21//Abstract: 22//provides platform-specific characters that are used to separate the directory level in a path string that reflects the hierarchical file system organization. The public static readonly char DirectorySeparatorChar; 24//25//Abstract: 26//provides a platform-specific array of characters that cannot be specified in the path string parameter passed to the member of the System.IO.Path class. 27//28///Return Result: 29//The character array of the invalid path character of the current platform. [Obsolete ("Please use Getinvalidpathchars or GetinvalidfilenamecHARs instead. ")] -public static readonly char[] InvalidPathChars; 32//33//Abstract: 34//platform-specific delimiter used to separate path strings in environment variables. public static readonly char PathSeparator; 36//37//Abstract: 38//provides platform-specific volume separators. The public static readonly char Volumeseparatorchar; 40 41//Abstract: 42//Change the extension of the path string. 43//44//Parameter: path:46///////To modify the path information. The path cannot contain any of the characters defined in System.IO.Path.GetInvalidPathChars (). EXTENSION:49/////////new extension (with or without a leading period). Specify NULL to remove the existing extension from path. 50//51///Return Result: 52//String containing the modified path information. On a Windows-based desktop platform, if path is null or an empty string (""), the path information returned is unmodified. If the extension is null, the returned string contains the specified path and its extension has been removed. If path does not have an extension, and extension is not NULL, the returned path string contains the extension///. Append, which is appended to the end of path. 55//56//Exception: £ º//SYSTEM.ARGUMENTEXCEPTION:58//path PackageContains one or more invalid characters that are already defined in System.IO.Path.GetInvalidPathChars (). A. public static string Changeextension (string path, string extension); 60//61//Abstract: 62//Combine a string array group into a path. 63//64//Parameter: +//paths:66//array consisting of the parts of the path. 67//68///Return Result: 69//String containing the merged path. 70//71//Exception: system.argumentexception:73//////The string in the array contains System.IO.Path.Ge One or more invalid characters defined in Tinvalidpathchars (). system.argumentnullexception:76////////////A string in the array is null. Combine public static string (params string[] paths); 78//79//Abstract: 80//combine two strings into one path. 81//82//Parameter: path1:84///////The first path to be combined. 88//89///Return Result: 90//String containing the merged path. If one of the specified paths is a 0-length string, the method returns a different path. If the path2 contains an absolute path, the partyThe law returns to path2. 91//92//exception: system.argumentexception:94///path1 or path2 contains System.IO.Pa Th. One or more invalid characters that are already defined in Getinvalidpathchars (). SYSTEM.ARGUMENTNULLEXCEPTION:97/////////path1 or path2 null. 98 public static string Combine (String path1, string path2); 99//100//Abstract: 101//combine three strings into one path. 102//103//Parameter: 104//path1:105//The first path to be combined. 106//107//path2:108//The second path to be combined. 109//110//path3:111//A third path to combine. 112//113//Return Result: 114//String containing the merged path. 115//116//exception: 117//system.argumentexception:118//path1, path2 or path3 includes Syste M.io. One or more invalid characters that are already defined in Path.getinvalidpathchars (). 119//120//system.argumentnullexception:121//path1, path2 or path3 is null. 122 public static string COmbine (String path1, String path2, String path3); 123//124//Abstract: 125//combine four strings into one path. 126//127//Parameters: +//path1:129//The first path to be combined. //131//path2:132//The second path to be combined. 133//134//path3:135//A third path to combine. 136//137//path4:138//fourth path to combine. 139//140//Return Result: 141//String containing the merged path.  142//143//exception: 144//system.argumentexception:145//path1, path2, Path3 or Path4 included One or more invalid characters that are already defined in System.IO.Path.GetInvalidPathChars (). 146//147//system.argumentnullexception:148//path1, path2, Path3, or Path4 are null.         149 public static string Combine (String path1, String path2, String path3, String path4); 150//151 Summary: 152//Returns directory information for the specified path string. 153//154//Parameter: 155//path:156//path of the file or directory. 157//158//Return Result: 159//System.String containing the path directory information, or null if path represents the root directory, is an empty string (""), or null 。 If the/path does not contain directory information, the System.String.Empty is returned. 161//162//exception: 163//system.argumentexception:164//The path parameter contains invalid characters, is empty, or contains only white space. 165//166//system.io.pathtoolongexception:167//The length of the path parameter exceeds the system-defined maximum length.         168 [securitysafecritical]169 public static string GetDirectoryName (string path); 170//171 Summary: 172//Returns the extension of the specified path string. 173//174//Parameter: 175///path:176///The path string from which the extension is obtained. 177//178//Return Result: 179//contains the extension of the specified path (including "." ) is System.String, NULL, or System.String.Empty. If path is null, then the//GetExtension returns NULL. If path does not have extension information, GetExtension returns EMPTY. 181//182//exception: 183//system.argumentexception:184//path contains System.IO.Path.GetIn ValidpathchaOne or more invalid characters that are already defined in RS (). 185 public static string GetExtension (string path); 186//187//Abstract: 188//Returns the text of the specified path string The name of the item and the extension. 189//190//Parameter: 191///path:192//The path string from which to get the file name and extension. 193//194//Return Result: 195//A System.String, consisting of the characters after the last directory character in Path. If the last character of path is a directory or volume delimiter, this method returns SYSTEM.STRING.EMPTY. If 196//path is NULL, this method returns NULL. 197//198//exception: 199//system.argumentexception:200//path contains System.IO.Path.GetIn One or more invalid characters that are already defined in Validpathchars (). 201 public static string GetFileName (string path); 202//203//Abstract: 204//Returns the specified path without an extension The file name of the path string. 205//206//parameter: 207//path:208//path of the file. 209//210//Return Result: 211//contains System.String of the string returned by System.IO.Path.GetFileName (System.String), But does not include the last period 212//(.) and all characters after the period.    213//214//exception: 215     system.argumentexception:216//path contains one or more invalid characters that are already defined in System.IO.Path.GetInvalidPathChars ().     217 public static string Getfilenamewithoutextension (string path); 218//219//Abstract: 220// Returns the absolute path of the specified path string. 221//222//Parameter: 223//path:224//The file or directory for which you want to get absolute path information. 225//226//return Result: 227//String containing the path's fully qualified location, such as "C:\MyFile.txt". 228//229//system.argumentexception:231//path is a 0-length string that contains only white space or contains Sys Tem. Io. One or more invalid characters have been defined in Path.getinvalidpathchars (). -232//or-the system failed to retrieve the absolute path. 233//234//system.security.securityexception:235//caller does not have the required permissions. 236//237//system.argumentnullexception:238//path is null. 239//240//system.notsupportedexception:241//path contains a colon (":"), this colon is not part of the volume identifier (for example, "\ C"). 242//243//System.IO.PathTooLongexception:244//The specified path, file name, or both exceed the system-defined maximum length. For example, on a Windows-based platform, the path must be less than 248 characters and the file name must be less than 260245//characters. 246 [securitysafecritical]247 public static string GetFullPath (string path); 248//249//Pick To: 250//Gets an array that contains characters that are not allowed in the file name. 251//252//Return Result: 253//contains an array of characters that are not allowed in the file name. 254 public static char[] Getinvalidfilenamechars (); 255//256//Abstract: 257//Get contains not allowed in path name The array of characters used in the. 258//259//Returns the result: 260//contains an array of characters that are not allowed in the path name. 261 public static char[] Getinvalidpathchars (); 262//263//Abstract: 264//Gets the root directory information for the specified path. 265//266//parameter: 267///path:268//The path from which to get the root directory information. 269//270//Return Result: 271//A string that contains the root of path, such as "C: \", or null if path is NULL, or if path does not contain root directory information, is an empty string. 272//273//exception: 274//system.argumentexception:275//PATh contains one or more invalid characters that are already defined in System.IO.Path.GetInvalidPathChars (). -or-system.string.empty276//is passed to path. 277 [securitysafecritical]278 public static string Getpathroot (string path); 279//280//Pick To: 281//Returns the random folder name or file name. 282//283//Return Result: 284//Random folder name or file name. 285 public static string Getrandomfilename (); 286//287//Abstract: 288//Create a uniquely named 0-byte temporary text on disk and return the full path to the file. 289//290//return Result: 291//System.String containing the full path of the temporary file. 292//293//Exception: 294//system.io.ioexception:295//An I/O error occurred, for example, no unique temporary file name was provided. -or-this method cannot create a temporary file.         296 [securitysafecritical]297 public static string GetTempFileName (); 298//299//Abstract: 300 Returns the path to the temporary folder for the current system. 301//302//Return Result: 303//System.String containing the path information for the temporary directory. 304//305//exception: 306//System.security.securityexception:307//The caller does not have the required permissions.         308 [securitysafecritical]309 public static string GetTempPath (); 310//311//Abstract: 312 Determines whether the path includes a file name extension. 313//314//Parameter: 315//path:316//path for search extension. 317//318//Return Result: 319//If the last directory delimiter in the path (\ \ or/) or the volume delimiter (:) The following character includes a period (.), and a character followed by one or more characters, or false. 320//321//exception: 322//system.argumentexception:323//path contains System.IO.Path.GetIn One or more invalid characters that are already defined in Validpathchars (). 324 public static bool Hasextension (string path); 325//326//Abstract: 327//Get a value that indicates the specified Whether the path string contains absolute path information or contains relative path information. 328//329//parameter: path:331/////the path to test. 332//333//Return Result: 334//True if path contains an absolute path, false otherwise. 335//336//exception: 337//system.argumentexception:338//path contains System.IO.Path.GetIn Validpathchars ()One or more invalid characters that are already defined in the. 339 public static bool IsPathRooted (string path); 340}341}

Get file full path, directory, extension, file name in C # path

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.