我们经常有遇到要处理文件路径的需求,那么一般我们常见的有几种:
首先我们创建了实例解决方案:
其中调用链是: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\