我们经常有遇到要处理文件路径的需求,那么一般我们常见的有几种:

  • 程序下面的文件
  • 临时目录下的文件

获取程序下面的文件

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

其中调用链是: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的更多相关文章

  1. Object-C,文件路径API

    犀利吐槽 1.同样都是"文件和目录操作",java中,就用java.util.File一个类,就封装了很多API,而Object-C搞了这么多类和函数.具体原因,有待分析啊. 2. ...

  2. python中读取文件数据时要注意文件路径

    我们在用python进行数据处理时往往需要将文件中的数据取出来做一些处理,这时我们应该注意数据文件的路径.文件路径不对,回报如下错误: FileNotFoundError: File b'..Adve ...

  3. Android用路径api在内部存储读写文件

    复制并修改原有项目 复制之前创建的项目CC+CV操作 需要改动的地方: * 项目名字 * 应用包名 * R文件重新导包 接着修改件/AndroidManifest.xml中的包名:package=&q ...

  4. LoadLibrary文件路径及windows API相关的文件路径问题

    LoadLibrary HMODULE WINAPI LoadLibrary( _In_  LPCTSTR lpFileName ); Loads the specified module into ...

  5. windows API实现用户选择文件路径的对话框

    在编写应用程序时,有时需要用户选择某个文件,以供应用程序使用,比如在某些管理程序中需要打开某一个进程,这个时候需要弹出一个对话框来将文件路径以树形图的形式表示出来,以图形化的方式供用户选择文件路径,而 ...

  6. Spring配置文件详解 - applicationContext.xml文件路径

    spring的配置文件applicationContext.xml的默认地址在WEB-INF下,只要在web.xml中加入代码 org.springframework.web.context.Cont ...

  7. Spring配置文件详解 – applicationContext.xml文件路径

    Spring配置文件详解 – applicationContext.xml文件路径 Java编程                 spring的配置文件applicationContext.xml的默 ...

  8. python 判断文件是否存在和删除文件的api (其中判断文件在不在让想起这个可以强兼容jenkins工作目录那个问题)

    判断文件在不在的api: os即operating system(操作系统),Python 的 os 模块封装了常见的文件和目录操作. os.path模块主要用于文件的属性获取,exists是“存在” ...

  9. java获取classpath文件路径空格转变成了转义字符%20的问题

    java获取classpath文件路径空格转变成了转义字符%20的问题 这个问题很纠结,服务器的文件路径带有空格,空格被转化是%20了,悲剧就出现了 下面展示一段代码String path = get ...

随机推荐

  1. Python-jet后台管理的使用

    python-django-jet库的使用 1.安装 pip install django-jet 2.配置 将'jet'应用添加到你的Django项目的设置文件settings.py中的INSTAL ...

  2. python使用try...except语句处理异常

    try....except语句语法格式: try: <语句> except(异常名称): <语句> 注意在except语句中的括号中的异常名称是可以省略的,当省略时就是全捕捉 ...

  3. Go语言中使用K8s API及一些常用API整理

    Go Client 在进入代码之前,理解k8s的go client项目是对我们又帮助的.它是k8s client中最古老的一个,因此具有很多特性. Client-go 没有使用Swagger生成器,就 ...

  4. Python 详解修饰器 附带 js使用修饰器

    修饰器 功能 修饰器的主要功能是,在不改变已有代码的情况下,为某一个类,方法等扩展功能 首先看这样一段代码 def foo(): for i in range(10): print(i) foo() ...

  5. java例题_01 不死神兔!

    1 /*1 [程序 1 不死神兔] 2 题目:古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少? 3 程 ...

  6. 死磕Spring之AOP篇 - Spring AOP自动代理(一)入口

    该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读. Spring 版本:5.1 ...

  7. 「HTML+CSS」--自定义加载动画【016】

    前言 Hello!小伙伴! 首先非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出- 哈哈 自我介绍一下 昵称:海轰 标签:程序猿一只|C++选手|学生 简介:因C语言结识编程,随后转入计算机 ...

  8. Message Decoding UVA - 213

     Some message encoding schemes require that an encoded message be sent in two parts. The fifirst par ...

  9. Day14_85_通过反射机制修改Class的属性值(IO+Properties)动态修改

    通过反射机制修改Class的属性值(IO+Properties)动态修改 import java.io.FileInputStream; import java.io.FileNotFoundExce ...

  10. forEach和map的用法和区别

    forEach()和map()都是处理数组的高阶函数有相同的三个值:(currentValue,index,arr): currentValue:必选,当前元素的值,index:可选,当前元素的下标, ...