最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#如何獲取當(dāng)前路徑的父路徑

 更新時間:2023年07月13日 08:41:54   作者:書香玫瑰  
這篇文章主要介紹了C#如何獲取當(dāng)前路徑的父路徑問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

C#如何獲取當(dāng)前路徑的父路徑

//獲取當(dāng)前運行路徑的上級目錄(父目錄)

 System.IO.DirectoryInfo topDir = System.IO.Directory.GetParent(System.Environment.CurrentDirectory);
//D:\項目\測試\test\bin\Debug

//繼續(xù)獲取上級的上級的上級的目錄。

string pathto = topDir.Parent.Parent.FullName;//D:\項目\測試\test

獲取路徑中最后一個文件夾的名字

string fileName = Path.GetFileNameWithoutExtension("E:\dyl190909\Demo\CulculateTest\Culculate\View.cs");
//fileName結(jié)果為Culculate

注:一個Parent獲取向上一級的目錄。

C#獲取當(dāng)前路徑7種方法

代碼

//獲取模塊的完整路徑。
string path1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
//獲取和設(shè)置當(dāng)前目錄(該進(jìn)程從中啟動的目錄)的完全限定目錄
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;
//獲取啟動了應(yīng)用程序的可執(zhí)行文件的路徑
string path6 = System.Windows.Forms.Application.StartupPath;
//獲取啟動了應(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等對象所確定的目錄而改變(切換工作目錄)。

每打開一次文件夾或者使用資源管理器查看一下文件,都會更改此方法獲得的值。

2、而System.Windows.Forms.Application.StartupPath或System.AppDomain.CurrentDomain.BaseDirectory可以獲得應(yīng)用程序運行所在的目錄,它是不隨你打開的文件夾而變的。

只跟應(yīng)用程序運行目錄有關(guān),其值等于應(yīng)用程序啟動的根目錄。

例如你安裝了程序在了C:\Program Files\程序文件夾 的位置下,那么他就是System.Windows.Forms.Application.StartupPath的值。

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

伊吾县| 武强县| 曲水县| 灵川县| 庆城县| 会泽县| 宜兰县| 绍兴市| 济阳县| 五莲县| 津市市| 九江市| 望城县| 郸城县| 松滋市| 洪湖市| 凤台县| 定陶县| 海伦市| 贵定县| 禹州市| 高青县| 大港区| 周至县| 湘阴县| 苏尼特左旗| 明光市| 元朗区| 即墨市| 滦平县| 顺昌县| 呈贡县| 侯马市| 白玉县| 葵青区| 金寨县| 大冶市| 金堂县| 棋牌| 朝阳市| 兴业县|