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)文章
DevExpress之ChartControl創(chuàng)建Drill-Down樣式的Title實(shí)例
這篇文章主要介紹了DevExpress之ChartControl創(chuàng)建Drill-Down樣式的Title實(shí)現(xiàn)方法,以實(shí)例形式講述了創(chuàng)建Drill-Down樣式的Title原理與實(shí)現(xiàn)過(guò)程,需要的朋友可以參考下2014-10-10
C#之HttpClient設(shè)置cookies的兩種方式
這篇文章主要介紹了C#之HttpClient設(shè)置cookies的兩種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11
解析C# Console 控制臺(tái)為什么也會(huì)卡死(原因分析)
在分析旅程中,總會(huì)有幾例控制臺(tái)的意外卡死導(dǎo)致的生產(chǎn)事故,有經(jīng)驗(yàn)的朋友都知道,控制臺(tái)卡死一般是動(dòng)了快速編輯窗口的緣故,雖然知道緣由,但一直沒(méi)有時(shí)間探究底層原理,市面上也沒(méi)有對(duì)這塊的底層原理介紹,昨天花了點(diǎn)時(shí)間簡(jiǎn)單探究了下,感興趣的朋友一起看看吧2023-10-10
C#中使用Join與GroupJoin將兩個(gè)集合進(jìn)行關(guān)聯(lián)與分組
這篇文章主要介紹了C#中使用Join與GroupJoin將兩個(gè)集合進(jìn)行關(guān)聯(lián)與分組,文中分別對(duì)Join和GroupJoin的用法進(jìn)行詳細(xì)說(shuō)明,需要的朋友可以參考下2017-12-12
C#實(shí)現(xiàn)兩個(gè)exe程序之間通信詳解
這篇文章主要為大家詳細(xì)介紹了C#如何使用SendMessage實(shí)現(xiàn)兩個(gè)程序之間的通信功能,文中的示例代碼簡(jiǎn)潔易懂,需要的小伙伴可以參考下2023-07-07

