c# Path路徑類


c# Path路徑類

Path類是static類型

常用方法

Path.GetFullPath(file) 取全路徑
Path.GetFileName(file) 取文件名,包含擴展名
Path.GetFileNameWithoutExtension(file) 取文件名,不包含擴展名
Path.GetExtension(file) 取擴展名
Path.GetDirectoryName(file) 取路徑名
Path.GetPathRoot(file)  取盤符
Path.Combine(file1,file2) 合並2個路徑

實現

string str = @"C:\Users\Administrator\Desktop\ceshi.txt";
            //獲得文件名
            Console.WriteLine(Path.GetFileName(str));
            //獲得不包含擴展名的文件名
            Console.WriteLine(Path.GetFileNameWithoutExtension(str));
            //獲得文件所在文件夾的名稱
            Console.WriteLine(Path.GetDirectoryName(str));
            //獲得文件所在的全路徑
            Console.WriteLine(Path.GetFullPath(str));
            //拼接路徑字符串
            Console.WriteLine(Path.Combine(@"D:\a\b\","c.txt"));
            Console.ReadKey();

屬性方法

屬性或方法 作用
string ChangeExtension(string path, string extension) 更改路徑字符串的擴展名
string Combine(params string[] paths) 將字符串數組組合成一個路徑
string Combine(string path1, string path2) 將兩個字符串組合成一個路徑
string GetDirectoryName(string path) 返回指定路徑字符串的目錄信息
string GetExtension(string path) 返回指定路徑字符串的擴展名
string GetFileName(string path) 返回指定路徑字符串的文件名和擴展名
string GetFileNameWithoutExtension(string path) 返回不具有擴展名的指定路徑字符串的文件名
string GetFullPath(string path) 返回指定路徑字符串的絕對路徑
char[] GetInvalidFileNameChars() 獲取包含不允許在文件名中使用的字符的數組
char[] GetInvalidPathChars() 獲取包含不允許在路徑名中使用的字符的數組
string GetPathRoot(string path) 獲取指定路徑的根目錄信息
string GetRandomFileName() 返回隨機文件夾名或文件名
string GetTempPath() 返回當前用戶的臨時文件夾的路徑
bool HasExtension(string path) 返回路徑是否包含文件的擴展名
bool IsPathRooted(string path) 返回路徑字符串是否包含根


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM