C# Path類---文件路徑解讀
C# Path類—文件路徑
給定如下字符串類型filePath,表示一個(gè)文件路徑:
string filePath = "D:\\Program\\Test\\Config.txt";
下面是Path類中的一些常用方法及執(zhí)行的結(jié)果:
Path.GetFullPath(filePath); ? //執(zhí)行結(jié)果為==>"D:\\Program\\Test\\Config.txt"; Path.GetDirectoryName(filePath); //執(zhí)行結(jié)果為==>D:\\Program\\Test Path.GetFileName(filePath); ?//執(zhí)行結(jié)果為==>Config.txt Path.GetFileNameWithoutExtension(filePath); //執(zhí)行結(jié)果為==>Config Path.GetExtension(filePath); //執(zhí)行結(jié)果為==>.txt Path.GetPathRoot(filePath); //執(zhí)行結(jié)果為==>D:\
獲取當(dāng)前的程序目錄:
AppDomain.CurrentDomain.BaseDirectory;?? ?//執(zhí)行結(jié)果==>"D:\\Program\\Test\\Bin\\Debug\\" Application.StartupPath;?? ?//執(zhí)行結(jié)果==>"D:\\Program\\Test\\Bin\\Debug" Environment.CurrentDirectory;//獲取和設(shè)置當(dāng)前目錄(該進(jìn)程從中啟動(dòng)的目錄)的完全限定目錄 Process.GetCurrentProcess().MainModule.FileName;//可獲得當(dāng)前執(zhí)行的exe的文件名
C# 文件路徑 Path類 測(cè)試
騰出點(diǎn)時(shí)間對(duì)Path類做一個(gè)系統(tǒng)的測(cè)試
? ? 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(); ? ? ? ? }?
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#實(shí)現(xiàn)向函數(shù)傳遞不定參數(shù)的方法
這篇文章主要介紹了C#實(shí)現(xiàn)向函數(shù)傳遞不定參數(shù)的方法,涉及C#操作函數(shù)參數(shù)的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04C#實(shí)現(xiàn)實(shí)體類與字符串互相轉(zhuǎn)換的方法
這篇文章主要介紹了C#實(shí)現(xiàn)實(shí)體類與字符串互相轉(zhuǎn)換的方法,涉及C#字符串及對(duì)象的相互轉(zhuǎn)換技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08C# 使用相同權(quán)限調(diào)用 cmd 傳入命令的方法
本文告訴大家如何使用相同權(quán)限調(diào)用cmd并且傳入命令,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-07C#訪問(wèn)PostGreSQL數(shù)據(jù)庫(kù)的方法
這次的項(xiàng)目中的一個(gè)環(huán)節(jié)要求我把PostGreSQL數(shù)據(jù)取出來(lái),然后放到SqlServer里,再去處理分析。2013-04-04C#(int)中Convert、Parse、TryParse的區(qū)別
Convert.ToInt32、int.Parse(Int32.Parse)、int.TryParse、(int) 四者都可以解釋為將類型轉(zhuǎn)換為 int,那它們的區(qū)別是什么呢?2013-04-04C#實(shí)現(xiàn)中文驗(yàn)證碼的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)中文驗(yàn)證碼功能,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以了解一下2022-12-12