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().Location,FooALibrary和FooBLibrary的入口都是Main.Shell - 获取调用该程序集的程序集路径:
Assembly.GetCallingAssembly().Location,当Main.Shell调FooBLibrary,输出Main.Shell,FooALibrary调FooBLibrary,输出FooALibrary
因此,用程序集Assembly的一些路径Api是非常灵活且准确的
获取临时目录下的文件
我们也经常会遇到需要获取临时目录路径的方式来放置一些程序临时文件,可以用下面方式获取:
Console.WriteLine(Path.GetTempPath());
//C:\Users\Ryzen\AppData\Local\Temp\
C#常见的文件路径Api的更多相关文章
- Object-C,文件路径API
犀利吐槽 1.同样都是"文件和目录操作",java中,就用java.util.File一个类,就封装了很多API,而Object-C搞了这么多类和函数.具体原因,有待分析啊. 2. ...
- python中读取文件数据时要注意文件路径
我们在用python进行数据处理时往往需要将文件中的数据取出来做一些处理,这时我们应该注意数据文件的路径.文件路径不对,回报如下错误: FileNotFoundError: File b'..Adve ...
- Android用路径api在内部存储读写文件
复制并修改原有项目 复制之前创建的项目CC+CV操作 需要改动的地方: * 项目名字 * 应用包名 * R文件重新导包 接着修改件/AndroidManifest.xml中的包名:package=&q ...
- LoadLibrary文件路径及windows API相关的文件路径问题
LoadLibrary HMODULE WINAPI LoadLibrary( _In_ LPCTSTR lpFileName ); Loads the specified module into ...
- windows API实现用户选择文件路径的对话框
在编写应用程序时,有时需要用户选择某个文件,以供应用程序使用,比如在某些管理程序中需要打开某一个进程,这个时候需要弹出一个对话框来将文件路径以树形图的形式表示出来,以图形化的方式供用户选择文件路径,而 ...
- Spring配置文件详解 - applicationContext.xml文件路径
spring的配置文件applicationContext.xml的默认地址在WEB-INF下,只要在web.xml中加入代码 org.springframework.web.context.Cont ...
- Spring配置文件详解 – applicationContext.xml文件路径
Spring配置文件详解 – applicationContext.xml文件路径 Java编程 spring的配置文件applicationContext.xml的默 ...
- python 判断文件是否存在和删除文件的api (其中判断文件在不在让想起这个可以强兼容jenkins工作目录那个问题)
判断文件在不在的api: os即operating system(操作系统),Python 的 os 模块封装了常见的文件和目录操作. os.path模块主要用于文件的属性获取,exists是“存在” ...
- java获取classpath文件路径空格转变成了转义字符%20的问题
java获取classpath文件路径空格转变成了转义字符%20的问题 这个问题很纠结,服务器的文件路径带有空格,空格被转化是%20了,悲剧就出现了 下面展示一段代码String path = get ...
随机推荐
- windows与远程linux服务器进行文件传输
在学习pwn时找到了http://pwnable.kr这个网站,很多题目通过ssh连接, ssh otp@pwnable.kr -p2222 (pw:guest) 连接脚本: pwn_ssh=ssh( ...
- 攻防世界 reverse 进阶 easyre-153
easyre-153 查壳: upx壳 脱壳: 1 int __cdecl main(int argc, const char **argv, const char **envp) 2 { 3 int ...
- 学《跟我一起写Makefile》笔记发博词
目录 笔记发博词 参考 笔记发博词 本系列笔记主要记录学了<跟我一起写Makefile>后的一些笔记 由于<跟我一起写Makefile>已经写得很详细了,所以我只是提取其中重要 ...
- CPU 权限划分
Intel的CPU将特权级别分为4个级别:RING0,RING1,RING2,RING3.Windows只使用其中的两个级别RING0和RING3,RING0只给操作系统用,RING3谁都能用.如果普 ...
- C# .NET Socket 简单实用框架,socket组件封装
参考资料 https://www.cnblogs.com/coldairarrow/p/7501645.html 根据.NET Socket 简单实用框架进行了改造,这个代码对socket通信封装还是 ...
- Kubernetes,kubectl常用命令详解
kubectl概述 祭出一张图,转载至 kubernetes-handbook/kubectl命令概述 ,可以对命令族有个整体的概念. 环境准备 允许master节点部署pod,使用命令如下: kub ...
- Shell中的(),{}几种语法用法-单独总结
shell中的(),{}几种语法用法 查看脚本语法是否有错误: bash -n modify_suffix.sh 跟踪执行 sh -x modify_suffix.sh aaa 1. ${var} 2 ...
- C++中的广义集合于for范围访问
在C++11中可以通过for在范围循环中访问广义集合 如: std::vetcor v={1,2,3}; int a[4] {1,2,3,4}; std::array<int,4>a2 { ...
- Dynamics CRM使用Web Api时如果参数里面包含"&"的时候的处理方法
当我们使用Dynamics CRM的Api的时候如果遇到查询字段的参数里面有&符号的话会影响Api的取值直接报错.原因是因为&符号在Url上面是一个关键字,这个关键字可以截断Url表示 ...
- Java String系列
String详解, String和CharSequence区别, StringBuilder和StringBuffer的区别 (String系列之1) StringBuilder 详解 (String ...