- A+
所属分类:.NET技术
我们经常有遇到要处理文件路径的需求,那么一般我们常见的有几种:
- 程序下面的文件
- 临时目录下的文件
获取程序下面的文件
首先我们创建了实例解决方案:
其中调用链是:Main.Shell->FooALibrary->,首先我们将FooAFolder.txt和FooA.txt的文件属性设置生成操作为内容,复制到输出目录为始终复制
那么我们有什么方法获取这两个文件的路径,我们可能会用到以下方法:
var currentDomainBaseDirectory = AppDomain.CurrentDomain.BaseDirectory; var result = File.Exists(Path.Combine(currentDomainBaseDirectory, @"FooAFolderFooAFolder.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, @"FooAFolderFooAFolder.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, @"FooAFolderFooAFolder.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, @"FooAFolderFooAFolder.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:UsersRyzensourcereposCommonFilePathApiSampleMain.ShellbinDebugnetcoreapp3.1FooBLibrary.dll ExecuteFooBGetCallingAssemblyPath: C:UsersRyzensourcereposCommonFilePathApiSampleMain.ShellbinDebugnetcoreapp3.1FooALibrary.dll GetExecutingAssemblyPath: C:UsersRyzensourcereposCommonFilePathApiSampleMain.ShellbinDebugnetcoreapp3.1FooBLibrary.dll GetCallingAssemblyPath: C:UsersRyzensourcereposCommonFilePathApiSampleMain.ShellbinDebugnetcoreapp3.1Main.Shell.dll GetEntryAssemblyPath: C:UsersRyzensourcereposCommonFilePathApiSampleMain.ShellbinDebugnetcoreapp3.1Main.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:UsersRyzenAppDataLocalTemp