C# Path類---文件路徑解讀
更新時間:2023年01月24日 14:12:52 作者:Danny_hi
這篇文章主要介紹了C# Path類---文件路徑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
C# Path類—文件路徑
給定如下字符串類型filePath,表示一個文件路徑:
string filePath = "D:\\Program\\Test\\Config.txt";
下面是Path類中的一些常用方法及執(zhí)行的結果:
Path.GetFullPath(filePath); ? //執(zhí)行結果為==>"D:\\Program\\Test\\Config.txt"; Path.GetDirectoryName(filePath); //執(zhí)行結果為==>D:\\Program\\Test Path.GetFileName(filePath); ?//執(zhí)行結果為==>Config.txt Path.GetFileNameWithoutExtension(filePath); //執(zhí)行結果為==>Config Path.GetExtension(filePath); //執(zhí)行結果為==>.txt Path.GetPathRoot(filePath); //執(zhí)行結果為==>D:\
獲取當前的程序目錄:
AppDomain.CurrentDomain.BaseDirectory;?? ?//執(zhí)行結果==>"D:\\Program\\Test\\Bin\\Debug\\" Application.StartupPath;?? ?//執(zhí)行結果==>"D:\\Program\\Test\\Bin\\Debug" Environment.CurrentDirectory;//獲取和設置當前目錄(該進程從中啟動的目錄)的完全限定目錄 Process.GetCurrentProcess().MainModule.FileName;//可獲得當前執(zhí)行的exe的文件名
C# 文件路徑 Path類 測試
騰出點時間對Path類做一個系統的測試
? ? private void PathTest()
? ? ? ? {
? ? ? ? ? ? //------------必須的空間-------using System.Diagnostics; ? ? using System.IO;
? ? ? ? ? ? string path = @"C:\Users\cks\Desktop\zzg\ERPWork1125\User.lua";
? ? ? ? ? ? Debug.Print(Path.ChangeExtension(path, "txt")); ? ? ? ? ? ? ? ? ? ? // 輸出:-----C:\Users\cks\Desktop\zzg\ERPWork1125\User.txt
? ? ? ? ? ? string path1 = @"C:\Users\cks\Desktop\zzg";
? ? ? ? ? ? string path2 = @"gg/e.txt";
? ? ? ? ? ? Debug.Print(Path.Combine(path1, path2)); ? ? ? ? ? ? ? ? ? ? ? ? ? ?//輸出:-----C:\Users\cks\Desktop\zzg\gg/e.txt
? ? ? ? ? ? Debug.Print(Path.GetDirectoryName(path)); ? ? ? ? ? ? ? ? ? ? ? ? ? //輸出:-----C:\Users\cks\Desktop\zzg\ERPWork1125
? ? ? ? ? ? Debug.Print(Path.GetExtension(path)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //輸出:-----.lua
? ? ? ? ? ? Debug.Print(Path.GetFileName(path)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//輸出:-----User.lua
? ? ? ? ? ? Debug.Print(Path.GetFileNameWithoutExtension(path)); ? ? ? ? ? ? ? ?//輸出:-----User
? ? ? ? ? ? Debug.Print(Path.GetFullPath(path)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?// 輸出:-----C:\Users\cks\Desktop\zzg\ERPWork1125\User.lua
? ? ? ? ? ? Debug.Print(String.Join("/x/", Path.GetInvalidFileNameChars())); ? ?//輸出:-----"/x/</x/>/x/|/x/
? ? ? ? ? ? Debug.Print(String.Join("/a/", Path.GetInvalidPathChars())); ? ? ? ?//輸出:----- " /a/</a/>/a/|/a/
? ? ? ? ? ? Debug.Print(Path.GetPathRoot(path)); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//輸出:-----C:\
? ? ? ? ? ? Debug.Print(Path.GetRandomFileName()); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//輸出:-----0am13z3o.gzd
? ? ? ? ? ? Debug.Print(Path.GetTempFileName()); ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//輸出:-----C:\Users\cks\AppData\Local\Temp\tmp81E5.tmp
? ? ? ? ? ? Debug.Print(Path.HasExtension(path).ToString()); ? ? ? ? ? ? ? ? ? ?//輸出:-----True
? ? ? ? ? ? Debug.Print(Path.IsPathRooted(path).ToString()); ? ? ? ? ? ? ? ? ? ?//輸出:-----True
? ? ? ? ? ? Debug.Print(Path.AltDirectorySeparatorChar.ToString()); ? ? ? ? ? ? //輸出:-----/
? ? ? ? ? ? Debug.Print(Path.DirectorySeparatorChar.ToString()); ? ? ? ? ? ? ? ?// ?輸出:-----\
? ? ? ? ? ? Debug.Print(String.Join("/x/", Path.InvalidPathChars)); ? ? ? ? ? ? //輸出:-----"/x/</x/>/x/|/x/
? ? ? ? ? ? Debug.Print(Path.PathSeparator.ToString()); ? ? ? ? ? ? ? ? ? ? ? ? //輸出:-----;
? ? ? ? ? ? Debug.Print(Path.VolumeSeparatorChar.ToString()); ? ? ? ? ? ? ? ? ? //輸出:-----: ?
? ? ? ? ? ? // LuaDLL.getc(stdin);
? ? ? ? ? ? //test t = new test();
? ? ? ? }?總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
DevExpress之ChartControl創(chuàng)建Drill-Down樣式的Title實例
這篇文章主要介紹了DevExpress之ChartControl創(chuàng)建Drill-Down樣式的Title實現方法,以實例形式講述了創(chuàng)建Drill-Down樣式的Title原理與實現過程,需要的朋友可以參考下2014-10-10
C#中使用Join與GroupJoin將兩個集合進行關聯與分組
這篇文章主要介紹了C#中使用Join與GroupJoin將兩個集合進行關聯與分組,文中分別對Join和GroupJoin的用法進行詳細說明,需要的朋友可以參考下2017-12-12

