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

c# 常見文件路徑Api的使用示例

 更新時間:2021年05月17日 08:35:45   作者:RyzenAdorer  
c#編程中經常有遇到要處理文件路徑的需求,本文分別講述了如何從程序下面的文件和臨時目錄下的文件去使用路徑api,感興趣的朋友可以了解下

獲取程序下面的文件

首先我們創(chuàng)建了實例解決方案:

其中調用鏈是:Main.Shell->FooALibrary->,首先我們將FooAFolder.txt和FooA.txt的文件屬性設置生成操作為內容,復制到輸出目錄為始終復制

那么我們有什么方法獲取這兩個文件的路徑,我們可能會用到以下方法:

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

主要用到的兩種方式就是:

  • 獲取應用程序域的基目錄:AppDomain.CurrentDomain.BaseDirectory
  • 獲取當前工作目錄的完全限定路徑:System.Environment.CurrentDirectory

但是實際上以上兩種方式不是最準和最穩(wěn)的,還有一種最穩(wěn)的方式:

獲取當前執(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

//通過反射獲取程序集
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

我們還能再拓展一下,我們在FooA和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();
    }
}

//調用
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,FooALibraryFooBLibrary的入口都是Main.Shell
  • 獲取調用該程序集的程序集路徑:Assembly.GetCallingAssembly().Location,當 Main.Shell調FooBLibrary,輸出Main.Shell,FooALibrary調FooBLibrary,輸出FooALibrary

因此,用程序集Assembly的一些路徑Api是非常靈活且準確的

獲取臨時目錄下的文件

我們也經常會遇到需要獲取臨時目錄路徑的方式來放置一些程序臨時文件,可以用下面方式獲?。?/p>

Console.WriteLine(Path.GetTempPath());
//C:\Users\Ryzen\AppData\Local\Temp\

以上就是c# 常見文件路徑Api的使用示例的詳細內容,更多關于c# 文件路徑Api的使用的資料請關注腳本之家其它相關文章!

相關文章

  • C# 中如何使用Thread

    C# 中如何使用Thread

    這篇文章主要介紹了C# 中使用 Thread的方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-01-01
  • C#二進制讀寫B(tài)inaryReader、BinaryWriter、BinaryFormatter

    C#二進制讀寫B(tài)inaryReader、BinaryWriter、BinaryFormatter

    這篇文章介紹了C#二進制讀寫B(tài)inaryReader、BinaryWriter、BinaryFormatter的用法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 解決C#獲取鼠標相對當前窗口坐標的實現方法

    解決C#獲取鼠標相對當前窗口坐標的實現方法

    本篇文章是對在C#中獲取鼠標相對當前窗口坐標的方法進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • c# 單例模式的實現

    c# 單例模式的實現

    這篇文章主要介紹了c# 單例模式的實現方法,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2021-02-02
  • c#各種Timer類的區(qū)別與用法介紹

    c#各種Timer類的區(qū)別與用法介紹

    System.Threading.Timer 是一個簡單的輕量計時器,它使用回調方法并由線程池線程提供服務。在必須更新用戶界面的情況下,建議不要使用該計時器,因為它的回調不在用戶界面線程上發(fā)生
    2013-10-10
  • C# ManagementObjectSearcher操作window案例詳解

    C# ManagementObjectSearcher操作window案例詳解

    這篇文章主要介紹了C# ManagementObjectSearcher操作window案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下
    2021-08-08
  • Unity實現俄羅斯方塊(二)

    Unity實現俄羅斯方塊(二)

    這篇文章主要為大家詳細介紹了Unity實現俄羅斯方塊的第一部分代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • C#中BackgroundWorker類用法總結

    C#中BackgroundWorker類用法總結

    本文詳細講解了C#中BackgroundWorker類用法總結,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-12-12
  • implicit關鍵字做自定義類型隱式轉換的方法

    implicit關鍵字做自定義類型隱式轉換的方法

    implicit 關鍵字用于聲明隱式的用戶定義類型轉換運算符。如果轉換過程可以確保不會造成數據丟失,則可使用該關鍵字在用戶定義類型和其他類型之間進行隱式轉換,這篇文章就給大家詳細介紹implicit關鍵字做自定義類型隱式轉換的方法,需要的朋友可以參考下
    2015-08-08
  • 基于C# winform實現圖片上傳功能的方法

    基于C# winform實現圖片上傳功能的方法

    這篇文章主要介紹了基于C# winform實現圖片上傳功能的方法,很實用的功能,需要的朋友可以參考下
    2014-07-07

最新評論

辽阳县| 康定县| 米脂县| 景宁| 雅江县| 潍坊市| 班戈县| 四子王旗| 和平县| 浪卡子县| 武鸣县| 安泽县| 界首市| 昌邑市| 微山县| 建宁县| 木兰县| 阿荣旗| 虹口区| 石柱| 娱乐| 沁水县| 江门市| 三门峡市| 常山县| 塘沽区| 深州市| 青海省| 本溪| 嘉鱼县| 和林格尔县| 五峰| 景德镇市| 永吉县| 安多县| 通州市| 广东省| 德惠市| 越西县| 玉门市| 大庆市|