c# 常見(jiàn)文件路徑Api的使用示例
獲取程序下面的文件
首先我們創(chuàng)建了實(shí)例解決方案:
其中調(diào)用鏈?zhǔn)牵篗ain.Shell->FooALibrary->,首先我們將FooAFolder.txt和FooA.txt的文件屬性設(shè)置生成操作為內(nèi)容,復(fù)制到輸出目錄為始終復(fù)制
那么我們有什么方法獲取這兩個(gè)文件的路徑,我們可能會(huì)用到以下方法:
var currentDomainBaseDirectory = AppDomain.CurrentDomain.BaseDirectory; var result = File.Exists(Path.Combine(currentDomainBaseDirectory, @"FooAFolder\FooAFolder.txt"))? "存在FooAFolder.txt": "不存在FooAFolder.txt"; Console.WriteLine(result); result = File.Exists(Path.Combine(currentDomainBaseDirectory, @"FooA.txt"))? "存在FooA.txt": "不存在FooA.txt"; Console.WriteLine(result); //存在FooAFolder.txt //存在FooA.txt var currentDirectory = System.Environment.CurrentDirectory; result=File.Exists(Path.Combine(currentDirectory, @"FooAFolder\FooAFolder.txt")) ? "存在FooAFolder.txt" : "不存在FooAFolder.txt"; Console.WriteLine(result); result = File.Exists(Path.Combine(currentDirectory, @"FooA.txt")) ? "存在FooA.txt" : "不存在FooA.txt"; Console.WriteLine(result); //存在FooAFolder.txt //存在FooA.txt
主要用到的兩種方式就是:
- 獲取應(yīng)用程序域的基目錄:
AppDomain.CurrentDomain.BaseDirectory
- 獲取當(dāng)前工作目錄的完全限定路徑:
System.Environment.CurrentDirectory
但是實(shí)際上以上兩種方式不是最準(zhǔn)和最穩(wěn)的,還有一種最穩(wěn)的方式:
獲取當(dāng)前執(zhí)行程序集的方式:Assembly.GetExecutingAssembly().Location(推薦方式)
var mainExecuteDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); result = File.Exists(Path.Combine(mainExecuteDirectory, @"FooAFolder\FooAFolder.txt")) ? "存在FooAFolder.txt" : "不存在FooAFolder.txt"; Console.WriteLine(result); result = File.Exists(Path.Combine(mainExecuteDirectory, @"FooA.txt")) ? "存在FooA.txt" : "不存在FooA.txt"; Console.WriteLine(result); //存在FooAFolder.txt //存在FooA.txt //通過(guò)反射獲取程序集 var fooAssembly = Assembly.GetAssembly(typeof(FooA)); var fooAExecuteDirectory = Path.GetDirectoryName(fooAssembly.Location); result = File.Exists(Path.Combine(fooAExecuteDirectory, @"FooAFolder\FooAFolder.txt")) ? "存在FooAFolder.txt" : "不存在FooAFolder.txt"; Console.WriteLine(result); result = File.Exists(Path.Combine(fooAExecuteDirectory, @"FooA.txt")) ? "存在FooA.txt" : "不存在FooA.txt"; Console.WriteLine(result); Console.ReadLine(); //存在FooAFolder.txt //存在FooA.txt
我們還能再拓展一下,我們?cè)贔ooA和FooB添加如下代碼:
public static class FooB { public static void GetExecutingAssemblyPath() { Console.WriteLine(Assembly.GetExecutingAssembly().Location); } public static void GetCallingAssemblyPath() { Console.WriteLine(Assembly.GetCallingAssembly().Location); } public static void GetEntryAssemblyPath() { Console.WriteLine(Assembly.GetEntryAssembly().Location); } } public static class FooA { public static void ExecuteFooBGetCallingAssemblyPath() { FooB.GetCallingAssemblyPath(); } public static void ExecuteFooBGetExecutingAssemblyPath() { FooB.GetExecutingAssemblyPath(); } } //調(diào)用 Console.WriteLine($"{nameof(FooA.ExecuteFooBGetExecutingAssemblyPath)}:"); FooA.ExecuteFooBGetExecutingAssemblyPath(); Console.WriteLine($"{nameof(FooA.ExecuteFooBGetCallingAssemblyPath)}:"); FooA.ExecuteFooBGetCallingAssemblyPath(); Console.WriteLine($"{nameof(FooB.GetExecutingAssemblyPath)}:"); FooB.GetExecutingAssemblyPath(); Console.WriteLine($"{nameof(FooB.GetCallingAssemblyPath)}:"); FooB.GetCallingAssemblyPath(); Console.WriteLine($"{nameof(FooB.GetEntryAssemblyPath)}:"); FooB.GetEntryAssemblyPath();
輸出:
ExecuteFooBGetExecutingAssemblyPath: C:\Users\Ryzen\source\repos\CommonFilePathApiSample\Main.Shell\bin\Debug\netcoreapp3.1\FooBLibrary.dll ExecuteFooBGetCallingAssemblyPath: C:\Users\Ryzen\source\repos\CommonFilePathApiSample\Main.Shell\bin\Debug\netcoreapp3.1\FooALibrary.dll GetExecutingAssemblyPath: C:\Users\Ryzen\source\repos\CommonFilePathApiSample\Main.Shell\bin\Debug\netcoreapp3.1\FooBLibrary.dll GetCallingAssemblyPath: C:\Users\Ryzen\source\repos\CommonFilePathApiSample\Main.Shell\bin\Debug\netcoreapp3.1\Main.Shell.dll GetEntryAssemblyPath: C:\Users\Ryzen\source\repos\CommonFilePathApiSample\Main.Shell\bin\Debug\netcoreapp3.1\Main.Shell.dl
我們從上面可以知道以下兩種的用法:
- 獲取入口程序集路徑:
Assembly.GetEntryAssembly().Location
,FooALibrary
和FooBLibrary
的入口都是Main.Shell
- 獲取調(diào)用該程序集的程序集路徑:
Assembly.GetCallingAssembly().Location
,當(dāng)Main.Shell
調(diào)FooBLibrary
,輸出Main.Shell
,FooALibrary
調(diào)FooBLibrary
,輸出FooALibrary
因此,用程序集Assembly的一些路徑Api是非常靈活且準(zhǔn)確的
獲取臨時(shí)目錄下的文件
我們也經(jīng)常會(huì)遇到需要獲取臨時(shí)目錄路徑的方式來(lái)放置一些程序臨時(shí)文件,可以用下面方式獲?。?/p>
Console.WriteLine(Path.GetTempPath()); //C:\Users\Ryzen\AppData\Local\Temp\
以上就是c# 常見(jiàn)文件路徑Api的使用示例的詳細(xì)內(nèi)容,更多關(guān)于c# 文件路徑Api的使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- C# 調(diào)用WebApi的實(shí)現(xiàn)
- c# 調(diào)用Win32Api關(guān)閉當(dāng)前應(yīng)用的方法
- c# 通過(guò)WinAPI播放PCM聲音
- c# webapi 配置swagger的方法
- C#調(diào)用Win32的API函數(shù)--User32.dll
- C#調(diào)用新浪微博API實(shí)例代碼
- C#調(diào)用百度API實(shí)現(xiàn)活體檢測(cè)的方法
- C# WebApi Get請(qǐng)求方式傳遞實(shí)體參數(shù)的方法示例
- C#凈化版WebApi框架的實(shí)現(xiàn)
- C# WebApi 路由機(jī)制剖析
- C# WebApi 接口傳參詳解
- c#在WebAPI使用Session的方法
相關(guān)文章
C#中一些你可能沒(méi)用過(guò)的調(diào)試窗口的方法
其他窗口比較常用,就不介紹了,是不是有一些你沒(méi)用到的窗口呢?2013-05-05C#中緊耦合Tight Coupling和松耦合Loose Coupling的實(shí)現(xiàn)
緊耦合和松耦合是描述模塊或組件之間耦合程度的兩個(gè)概念,本文主要介紹了C#中緊耦合Tight Coupling和松耦合Loose Coupling的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01使用C#調(diào)用百度地圖并實(shí)現(xiàn)坐標(biāo)點(diǎn)的設(shè)置以及讀取示例
這篇文章主要介紹了使用C#調(diào)用百度地圖并實(shí)現(xiàn)坐標(biāo)點(diǎn)的設(shè)置以及讀取示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07c# Newtonsoft 六個(gè)值得使用的特性(上)
這篇文章主要介紹了c# Newtonsoft 六個(gè)值得使用的特性,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-06-06WPF使用觸發(fā)器需要注意優(yōu)先級(jí)問(wèn)題解決
本文主要介紹了WPF使用觸發(fā)器需要注意優(yōu)先級(jí)問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01unity實(shí)現(xiàn)場(chǎng)景跳轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)場(chǎng)景跳轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04