我们经常有遇到要处理文件路径的需求,那么一般我们常见的有几种:
程序下面的文件
临时目录下的文件
获取程序下面的文件首先我们创建了实例解决方案:
其中调用链是: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是非常灵活且准确的
获取临时目录下的文件