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

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

获取程序下面的文件

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

其中调用链是: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. 《Selenium自动化测试实战:基于Python》之 Python与Selenium环境的搭建

    第2章  Python与Selenium环境的搭建 购买链接:  京东:https://item.jd.com/13123910.html  当当:http://product.dangdang.co ...

  2. python编写自己的base64加解密工具

    0x00 Base64编码的用途 在网络传输中,不是所的的内容都是可打印字符,其中绝大多数数据是不可见字符,base64可以基于64个可打印字符来表示这些带有不可打印字符的传输数据. 0x01 Bas ...

  3. 菜刀jsp小马

    逛google收获小马一枚,收藏一下 <%@page import="java.io.*,java.util.*,java.net.*,java.sql.*,java.text.*&q ...

  4. 使用C# (.NET Core) 实现模板方法模式 (Template Method Pattern)

    本文的概念内容来自深入浅出设计模式一书. 项目需求 有一家咖啡店, 供应咖啡和茶, 它们的工序如下: 咖啡: 茶: 可以看到咖啡和茶的制作工序是差不多的, 都是有4步, 其中有两步它们两个是一样的, ...

  5. GO基础知识分享

    目录 GO基础知识分享 go语言的基本事项 关键字 字符串的拼接和变量的定义方式 空白符 const常量 iota的用法 运算符 Go 没有三目运算符,不能适用?: 语言条件语句 for循环的方式 函 ...

  6. 全网最值得推荐的ELKB日志学习博客-博客地址留存

    博客地址:https://elasticstack.blog.csdn.net/article/details/102728604 博客地址留存,后续解决疑难问题

  7. BUAA_OS lab2 难点梳理

    BUAA_OS lab2 难点梳理 实验重点 所列出的实验重点为笔者在进行lab2过程中认为需要深刻理解的部分. 进行内存访问的流程 熟悉mips内存映射布局,即理解mmu.h内图 二级页表的理解和实 ...

  8. Linux 查看GPU状态

    Linux 查看GPU状态 nvidia-smi nvidia-smi是NVIDIA自带的一个命令可以详细的展示显卡的运行状态. gpustat gpustat是github上开源的一个小工具,对于v ...

  9. Ambassador-04- Mapping 资源

    官方文档:https://www.getambassador.io/docs/latest/topics/using/intro-mappings/#resources Ambassador 通过Ma ...

  10. (二十)VMware Harbor - API

    可以用swagger在线解析 http://editor.swagger.io/将swagger.yaml中的内容拷贝到里面即可. 官方文档说明链接如下:https://github.com/vmwa ...