C# Path類---文件路徑解讀
更新時間:2023年01月24日 14:12:52 作者:Danny_hi
這篇文章主要介紹了C# Path類---文件路徑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
C# Path類—文件路徑
給定如下字符串類型filePath,表示一個文件路徑:
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:\
獲取當前的程序目錄:
AppDomain.CurrentDomain.BaseDirectory;?? ?//執(zhí)行結(jié)果==>"D:\\Program\\Test\\Bin\\Debug\\" Application.StartupPath;?? ?//執(zhí)行結(jié)果==>"D:\\Program\\Test\\Bin\\Debug" Environment.CurrentDirectory;//獲取和設置當前目錄(該進程從中啟動的目錄)的完全限定目錄 Process.GetCurrentProcess().MainModule.FileName;//可獲得當前執(zhí)行的exe的文件名
C# 文件路徑 Path類 測試
騰出點時間對Path類做一個系統(tǒng)的測試
? ? 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é)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
C#實現(xiàn)向函數(shù)傳遞不定參數(shù)的方法
這篇文章主要介紹了C#實現(xiàn)向函數(shù)傳遞不定參數(shù)的方法,涉及C#操作函數(shù)參數(shù)的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下2015-04-04C#實現(xiàn)實體類與字符串互相轉(zhuǎn)換的方法
這篇文章主要介紹了C#實現(xiàn)實體類與字符串互相轉(zhuǎn)換的方法,涉及C#字符串及對象的相互轉(zhuǎn)換技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-08-08C# 使用相同權(quán)限調(diào)用 cmd 傳入命令的方法
本文告訴大家如何使用相同權(quán)限調(diào)用cmd并且傳入命令,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友參考下吧2018-07-07C#(int)中Convert、Parse、TryParse的區(qū)別
Convert.ToInt32、int.Parse(Int32.Parse)、int.TryParse、(int) 四者都可以解釋為將類型轉(zhuǎn)換為 int,那它們的區(qū)別是什么呢?2013-04-04