class Program { static void Main(string[] args) { //獲取當前運行程序的目錄 string fileDir = Environment.CurrentDirectory; Console.WriteLine("當前程序目錄:"+fileDir); //一個文件目錄 string filePath = "C:\\bin\\files\\test.xml"; Console.WriteLine("該文件的目錄:"+filePath); string str = "獲取文件的全路徑:" + Path.GetFullPath(filePath); //-->C:\bin\files\test.xml Console.WriteLine(str); str = "獲取文件所在的目錄:" + Path.GetDirectoryName(filePath); //-->C:\bin\files Console.WriteLine(str); str = "獲取文件的名稱含有後綴:" + Path.GetFileName(filePath); //-->test.xml Console.WriteLine(str); str = "獲取文件的名稱沒有後綴:" + Path.GetFileNameWithoutExtension(filePath); //-->test Console.WriteLine(str); str = "獲取路徑的後綴擴展名稱:" + Path.GetExtension(filePath); //-->.xml Console.WriteLine(str); str = "獲取路徑的根目錄:" + Path.GetPathRoot(filePath); //-->C:\ Console.WriteLine(str); Console.ReadKey(); } }
說明 | |
---|---|
ChangeExtension(String, String) |
更改路徑字符串的擴展名。數組 |
Combine(String, String) |
將兩個字符串組合成一個路徑。app |
Combine(String, String, String) |
將三個字符串組合成一個路徑。dom |
Combine(String, String, String, String) |
將四個字符串組合成一個路徑。ide |
Combine(String[]) |
將字符串數組組合成一個路徑。ui |
GetDirectoryName(String) |
返回指定路徑字符串的目錄信息。spa |
GetExtension(String) |
返回指定的路徑字符串的擴展名。3d |
GetFileName(String) |
返回指定路徑字符串的文件名和擴展名。code |
GetFileNameWithoutExtension(String) |
返回不具備擴展名的指定路徑字符串的文件名。orm |
GetFullPath(String) |
返回指定路徑字符串的絕對路徑。xml |
GetInvalidFileNameChars() |
獲取包含不容許在文件名中使用的字符的數組。 |
GetInvalidPathChars() |
獲取包含不容許在路徑名中使用的字符的數組。 |
GetPathRoot(String) |
獲取指定路徑的根目錄信息。 |
GetRandomFileName() |
返回隨機文件夾名或文件名。 |
GetTempFileName() |
在磁盤上建立磁惟一命名的零字節的臨時文件並返回該文件的完整路徑。 |
GetTempPath() |
返回當前用戶的臨時文件夾的路徑。 |
HasExtension(String) |
肯定路徑是否包括文件擴展名。 |
IsPathRooted(String) |
獲取一個值,該值指示指定的路徑字符串是否包含根。 |
名稱 | 說明 |
---|---|
AltDirectorySeparatorChar |
提供平臺特定的替換字符,該替換字符用於在反映分層文件系統組織的路徑字符串中分隔目錄級別。 |
DirectorySeparatorChar |
提供平臺特定的字符,該字符用於在反映分層文件系統組織的路徑字符串中分隔目錄級別。 |
InvalidPathChars |
已過期。 提供平臺特定的字符數組,這些字符不能在傳遞到 Path 類的成員的路徑字符串參數中指定。 |
PathSeparator |
用於在環境變量中分隔路徑字符串的平臺特定的分隔符。 |
VolumeSeparatorChar |
提供平臺特定的卷分隔符。 |