神刀安全网

C# Path

In this article, we will discuss C# Path directive and different methods in C# Path

A path is a string that provides the location of a file or directory. A path does not necessarily point to a location on disk.A path can contain absolute or relative location information. Absolute paths fully specify a location: the file or directory can be uniquely identified regardless of the current location. Relative paths specify a partial location: the current location is used as the starting point when locating a file specified with a relative path.System.IO namespace supports this class.

Methods:

ChangeExtension(String, String):Changes the extension of a path string.

Combine(String, String):Combines two strings into a path.

Combine(String[]):Combines an array of strings into a path.

GetDirectoryName(String):Returns the directory information for the specified path string.

GetExtension(String):Returns the extension of the specified path string.

GetFileName(String):Returns the file name and extension of the specified path string.

GetFileNameWithoutExtension(String):Returns the file name of the specified path string without the extension.

GetFullPath(String):Returns the absolute path for the specified path string.

GetInvalidFileNameChars():Gets an array containing the characters that are not allowed in file names.

GetInvalidPathChars():Gets an array containing the characters that are not allowed in path names.

GetPathRoot(String):Gets the root directory information of the specified path.

GetRandomFileName():Returns a random folder name or file name.

GetTempFileName():Creates a uniquely named, zero-byte temporary file on disk and returns the full path of that file.

GetTempPath():Returns the path of the current user’s temporary folder.

HasExtension(String):Determines whether a path includes a file name extension.

IsPathRooted(String):Gets a value indicating whether the specified path string contains a root.

Example:

class Program     {       static void Main(string[] args)         {             string path = "C://csharpstar.txt";             string changeextension = Path.ChangeExtension(path, ".doc");             Console.WriteLine(changeextension);             string directoryname = Path.GetDirectoryName(path);             Console.WriteLine(directoryname);             string extension = Path.GetExtension(path);             Console.WriteLine(extension);             string filename = Path.GetFileName(path);             Console.WriteLine(filename);             string filenameNoExtension = Path.GetFileNameWithoutExtension(path);             Console.WriteLine(filenameNoExtension);             string root = Path.GetPathRoot(path);             Console.WriteLine(root);             string fullpath = Path.GetFullPath(path);             Console.WriteLine(fullpath);             Console.ReadLine();         }     } 

Output:

C# Path

Thanks for visiting !!

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » C# Path

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
分享按钮