C#文件路徑獲取函數和文件名字獲取函數

xb3b 9年前發布 | 900 次閱讀 C#

1. 獲取絕對文件路徑

System.IO.Path.GetFullPath(string path)

string fileName = "myfile.ext";
string path1 = @"mydir";
string path2 = @"\mydir";
string fullPath;

fullPath = Path.GetFullPath(path1);
fullPath = Path.GetFullPath(fileName);
fullPath = Path.GetFullPath(path2);

2. 獲取文件名字(得到指定路徑內的文件名,不包括擴展名)

System.IO.Path.GetFileNameWithoutExtension(string path)
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;
result = Path.GetFileNameWithoutExtension(fileName);
result = Path.GetFileName(path);

3.獲得包含 path 目錄信息的string 或者為空引用

System.IO.Path.GetDirectoryName(string path)

string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string rootPath = @"C:\";
string directoryName;
    
directoryName = Path.GetDirectoryName(fileName);
directoryName = Path.GetDirectoryName(path);
directoryName = Path.GetDirectoryName(rootPath);

4.合并兩個路徑字符串。

System.IO.Path.Combine(String path1, String path2)

 本文由用戶 xb3b 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!