c# 常见文件路径Api的使用示例

 更新时间:2021年05月17日 08:35:45   作者:RyzenAdorer  
c#编程中经常有遇到要处理文件路径的需求,本文分别讲述了如何从程序下面的文件和临时目录下的文件去使用路径api,感兴趣的朋友可以了解下

获取程序下面的文件

首先我们创建了实例解决方案:

其中调用链是: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

但是实际上以上两种方式不是最准和最稳的,还有一种最稳的方式:

获取当前执行程序集的方式: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().LocationFooALibraryFooBLibrary的入口都是Main.Shell
  • 获取调用该程序集的程序集路径:Assembly.GetCallingAssembly().Location,当 Main.ShellFooBLibrary,输出Main.ShellFooALibraryFooBLibrary,输出FooALibrary

因此,用程序集Assembly的一些路径Api是非常灵活且准确的

获取临时目录下的文件

我们也经常会遇到需要获取临时目录路径的方式来放置一些程序临时文件,可以用下面方式获取:

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#二进制读写BinaryReader、BinaryWriter、BinaryFormatter

    C#二进制读写BinaryReader、BinaryWriter、BinaryFormatter

    这篇文章介绍了C#二进制读写BinaryReader、BinaryWriter、BinaryFormatter的用法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • 解决C#获取鼠标相对当前窗口坐标的实现方法

    解决C#获取鼠标相对当前窗口坐标的实现方法

    本篇文章是对在C#中获取鼠标相对当前窗口坐标的方法进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • c# 单例模式的实现

    c# 单例模式的实现

    这篇文章主要介绍了c# 单例模式的实现方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下
    2021-02-02
  • c#各种Timer类的区别与用法介绍

    c#各种Timer类的区别与用法介绍

    System.Threading.Timer 是一个简单的轻量计时器,它使用回调方法并由线程池线程提供服务。在必须更新用户界面的情况下,建议不要使用该计时器,因为它的回调不在用户界面线程上发生
    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

最新评论