欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# 常見(jiàn)文件路徑Api的使用示例

 更新時(shí)間:2021年05月17日 08:35:45   作者:RyzenAdorer  
c#編程中經(jīng)常有遇到要處理文件路徑的需求,本文分別講述了如何從程序下面的文件和臨時(shí)目錄下的文件去使用路徑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().LocationFooALibraryFooBLibrary的入口都是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)文章!

相關(guān)文章

  • C#中一些你可能沒(méi)用過(guò)的調(diào)試窗口的方法

    C#中一些你可能沒(méi)用過(guò)的調(diào)試窗口的方法

    其他窗口比較常用,就不介紹了,是不是有一些你沒(méi)用到的窗口呢?
    2013-05-05
  • C#中緊耦合Tight Coupling和松耦合Loose Coupling的實(shí)現(xiàn)

    C#中緊耦合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è)置以及讀取示例

    這篇文章主要介紹了使用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-07
  • 枚舉的用法詳細(xì)總結(jié)

    枚舉的用法詳細(xì)總結(jié)

    本篇文章主要是對(duì)枚舉的用法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2014-01-01
  • c# Newtonsoft 六個(gè)值得使用的特性(上)

    c# Newtonsoft 六個(gè)值得使用的特性(上)

    這篇文章主要介紹了c# Newtonsoft 六個(gè)值得使用的特性,文中示例代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • 在Unity中使用全局變量的操作

    在Unity中使用全局變量的操作

    這篇文章主要介紹了在Unity中使用全局變量的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-04-04
  • WPF使用觸發(fā)器需要注意優(yōu)先級(jí)問(wèn)題解決

    WPF使用觸發(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-01
  • C#實(shí)現(xiàn)兩個(gè)日期比較大小

    C#實(shí)現(xiàn)兩個(gè)日期比較大小

    這篇文章主要為大家詳細(xì)介紹了C#中實(shí)現(xiàn)兩個(gè)日期比較大小的常見(jiàn)方法,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-12-12
  • unity實(shí)現(xiàn)場(chǎng)景跳轉(zhuǎn)

    unity實(shí)現(xiàn)場(chǎng)景跳轉(zhuǎn)

    這篇文章主要為大家詳細(xì)介紹了unity實(shí)現(xiàn)場(chǎng)景跳轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • C#解析JSON實(shí)例

    C#解析JSON實(shí)例

    這篇文章主要介紹了C#解析JSON的方法,主要采用了C#自帶的JavaScriptSerializer類來(lái)實(shí)現(xiàn),方法簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2014-09-09

最新評(píng)論