C#如何獲取當(dāng)前路徑的父路徑
C#如何獲取當(dāng)前路徑的父路徑
//獲取當(dāng)前運(yùn)行路徑的上級(jí)目錄(父目錄)
System.IO.DirectoryInfo topDir = System.IO.Directory.GetParent(System.Environment.CurrentDirectory); //D:\項(xiàng)目\測(cè)試\test\bin\Debug
//繼續(xù)獲取上級(jí)的上級(jí)的上級(jí)的目錄。
string pathto = topDir.Parent.Parent.FullName;//D:\項(xiàng)目\測(cè)試\test
獲取路徑中最后一個(gè)文件夾的名字
string fileName = Path.GetFileNameWithoutExtension("E:\dyl190909\Demo\CulculateTest\Culculate\View.cs"); //fileName結(jié)果為Culculate
注:一個(gè)Parent獲取向上一級(jí)的目錄。
C#獲取當(dāng)前路徑7種方法
代碼
//獲取模塊的完整路徑。 string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; //獲取和設(shè)置當(dāng)前目錄(該進(jìn)程從中啟動(dòng)的目錄)的完全限定目錄 string path2 = System.Environment.CurrentDirectory; //獲取應(yīng)用程序的當(dāng)前工作目錄 string path3 = System.IO.Directory.GetCurrentDirectory(); //獲取程序的基目錄 string path4 = System.AppDomain.CurrentDomain.BaseDirectory; //獲取和設(shè)置包括該應(yīng)用程序的目錄的名稱 string path5 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase; //獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑 string path6 = System.Windows.Forms.Application.StartupPath; //獲取啟動(dòng)了應(yīng)用程序的可執(zhí)行文件的路徑及文件名 string path7 = System.Windows.Forms.Application.ExecutablePath; StringBuilder str=new StringBuilder(); str.AppendLine("System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:" + path1); str.AppendLine("System.Environment.CurrentDirectory:" + path2); str.AppendLine("System.IO.Directory.GetCurrentDirectory():" + path3); str.AppendLine("System.AppDomain.CurrentDomain.BaseDirectory:" + path4); str.AppendLine("System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:" + path5); str.AppendLine("System.Windows.Forms.Application.StartupPath:" + path6); str.AppendLine("System.Windows.Forms.Application.ExecutablePath:" + path7); string allPath = str.ToString();
輸出結(jié)果
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.vshost.exe
System.Environment.CurrentDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.IO.Directory.GetCurrentDirectory():D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.AppDomain.CurrentDomain.BaseDirectory:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\
System.Windows.Forms.Application.StartupPath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release
System.Windows.Forms.Application.ExecutablePath:D:\work\prj\VP-VPlatform\XmlAndXsd\bin\Release\XmlAndXsd.EXE
用法須知
1、Net Framework中System.IO.Directory.GetCurrentDirectory()方法用于獲得應(yīng)用程序當(dāng)前工作目錄。
如果使用此方法獲得應(yīng)用程序所在的目錄,應(yīng)該注意:System.IO.Directory.GetCurrentDirectory()方法獲得的目錄路徑隨著OpenFileDialog、SaveFileDialog等對(duì)象所確定的目錄而改變(切換工作目錄)。
每打開(kāi)一次文件夾或者使用資源管理器查看一下文件,都會(huì)更改此方法獲得的值。
2、而System.Windows.Forms.Application.StartupPath或System.AppDomain.CurrentDomain.BaseDirectory可以獲得應(yīng)用程序運(yùn)行所在的目錄,它是不隨你打開(kāi)的文件夾而變的。
只跟應(yīng)用程序運(yùn)行目錄有關(guān),其值等于應(yīng)用程序啟動(dòng)的根目錄。
例如你安裝了程序在了C:\Program Files\程序文件夾 的位置下,那么他就是System.Windows.Forms.Application.StartupPath的值。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解C#如何使用重載方法實(shí)現(xiàn)不同類型數(shù)據(jù)的計(jì)算
這篇文章主要為大家詳細(xì)介紹了C#如何使用重載方法實(shí)現(xiàn)不同類型數(shù)據(jù)的計(jì)算,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02C#調(diào)用C++的dll兩種實(shí)現(xiàn)方式(托管與非托管)
這篇文章主要介紹了C#調(diào)用C++的dll兩種實(shí)現(xiàn)方式(托管與非托管),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08深入解析C#中的交錯(cuò)數(shù)組與隱式類型的數(shù)組
這篇文章主要介紹了深入解析C#中的交錯(cuò)數(shù)組與隱式類型的數(shù)組,隱式類型的數(shù)組通常與匿名類型以及對(duì)象初始值設(shè)定項(xiàng)和集合初始值設(shè)定項(xiàng)一起使用,需要的朋友可以參考下2016-01-01C# Winform使用log4net進(jìn)行日志記錄
Log4Net是從Java的log4j移植過(guò)來(lái)的,功能也與log4j類似,可以把日志信息輸出到文件、數(shù)據(jù)庫(kù)等不同的介質(zhì)或目標(biāo),下面我們就來(lái)學(xué)習(xí)一下如何使用log4net進(jìn)行日志記錄吧2023-11-11C#中使用Cache框架快速實(shí)現(xiàn)Cache操作
這篇文章介紹了C#中使用Cache框架快速實(shí)現(xiàn)Cache操作的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06