C# System.IO.Path
System.IO.Path.GetExtension
返回指定的路径字符串的扩展名。
string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3"; Console.WriteLine(System.IO.Path.GetExtension(file1)); //.mp3 string file2 = @"Users\Administrator\Desktop\temp\12345.mp3.mp4"; Console.WriteLine(System.IO.Path.GetExtension(file2)); //.mp4
System.IO.Path.ChangeExtension
更改路径字符串的扩展名。
string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3"; Console.WriteLine(System.IO.Path.ChangeExtension(file1,".mp5")); //C:\Users\Administrator\Desktop\temp\12345.mp5 string file2 = @"Users\Administrator\Desktop\temp\12345.mp3.mp4"; Console.WriteLine(System.IO.Path.ChangeExtension(file2,".mp5")); //Users\Administrator\Desktop\temp\12345.mp3.mp5
System.IO.Path.Combine
将多个字符串组合成一个路径。
string file1 = @"C:\Users\Administrator\Desktop\temp\"; string file2 = @"12345.mp3"; Console.WriteLine(System.IO.Path.Combine(file1,file2)); //C:\Users\Administrator\Desktop\temp\12345.mp3
Combine( String[] ) | 将字符串数组组合成一个路径。 |
Combine(String, String) | 将两个字符串组合成一个路径。 |
Combine(String, String, String) | 将三个字符串组合成一个路径。 |
Combine(String, String, String, String) | 将四个字符串组合成一个路径。 |
System.IO.Path.GetDirectoryName
返回指定路径字符串的目录信息。
string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3"; Console.WriteLine(System.IO.Path.GetDirectoryName(file1)); //C:\Users\Administrator\Desktop\temp string file2 = @"Users\Administrator\Desktop\temp\12345.mp3.mp4"; Console.WriteLine(System.IO.Path.GetDirectoryName(file2)); //Users\Administrator\Desktop\temp
System.IO.Path.GetFileName
返回指定路径字符串的文件名和扩展名。
string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3"; Console.WriteLine(System.IO.Path.GetFileName(file1)); //12345.mp3 string file2 = @"C:\Users\Administrator\Desktop\temp\12345.mp3.mp4"; Console.WriteLine(System.IO.Path.GetFileName(file2)); //12345.mp3.mp4
System.IO.Path.GetFileNameWithoutExtension
返回不具有扩展名的指定路径字符串的文件名。
string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3"; Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file1)); //12345 string file2 = @"C:\Users\Administrator\Desktop\temp\12345.mp3.mp4"; Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(file2)); //12345.mp3
System.IO.Path.GetFullPath
返回指定路径字符串的绝对路径。
string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3"; Console.WriteLine(System.IO.Path.GetFullPath(file1)); //C:\Users\Administrator\Desktop\temp\12345.mp3 string file2 = @"C:\Users\Administrator\Desktop\temp\12345.mp3.mp4"; Console.WriteLine(System.IO.Path.GetFullPath(file2)); //C:\Users\Administrator\Desktop\temp\12345.mp3.mp4
System.IO.Path.GetPathRoot
获取指定路径的根目录信息。
string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3"; Console.WriteLine(System.IO.Path.GetPathRoot(file1)); //C:\ string file2 = @"C:\Users\Administrator\Desktop\temp\12345.mp3.mp4"; Console.WriteLine(System.IO.Path.GetPathRoot(file2)); //C:\
System.IO.Path.GetRandomFileName
返回随机文件夹名或文件名。
Console.WriteLine(System.IO.Path.GetRandomFileName()); //4gi0u4hz.10w
System.IO.Path.GetTempFileName
在磁盘上创建磁唯一命名的零字节的临时文件并返回该文件的完整路径。
Console.WriteLine(System.IO.Path.GetTempFileName()); //C:\Users\Administrator\AppData\Local\Temp\tmp4C97.tmp
System.IO.Path.GetTempPath
返回当前用户的临时文件夹的路径。
Console.WriteLine(System.IO.Path.GetTempPath()); //C:\Users\Administrator\AppData\Local\Temp
System.IO.Path.HasExtension
确定路径是否包括文件扩展名。
string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3"; Console.WriteLine(System.IO.Path.HasExtension(file1)); //True string file2 = @"C:\Users\Administrator\Desktop\temp\12345.mp3.mp4"; Console.WriteLine(System.IO.Path.HasExtension(file2)); //True string file3 = @"C:\Users\Administrator\Desktop\temp\12345"; Console.WriteLine(System.IO.Path.HasExtension(file3)); //False
System.IO.Path.IsPathRooted
获取一个值,该值指示指定的路径字符串是否包含根。
string file1 = @"C:\Users\Administrator\Desktop\temp\12345.mp3"; Console.WriteLine(System.IO.Path.IsPathRooted(file1)); //True string file2 = @"Users\Administrator\Desktop\temp\12345.mp3.mp4"; Console.WriteLine(System.IO.Path.IsPathRooted(file2)); //False
System.IO.Path.GetInvalidPathChars
获取包含不允许在路径名中使用的字符的数组。
foreach (int arg in System.IO.Path.GetInvalidPathChars()) { Console.WriteLine(arg); } /* 34 60 62 124 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 */
System.IO.Path.GetInvalidFileNameChars
获取包含不允许在文件名中使用的字符的数组。
foreach (int arg in System.IO.Path.GetInvalidFileNameChars()) { Console.WriteLine(arg); } /* 34 60 62 124 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 58 42 63 92 47 */
转载请注明出处。