测试环境
主要对.Net Core下的几种获取文件方式进行测试,
测试环境
.Net Standard 2.0 类库 ClassLibrary1
.Net Core 3.1控制台程序 LinuxPathTest
发布分别打包为linux-x64和win-x64 无依赖 单文件 裁剪未使用程序集
测试代码
ClassLibrary1
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace ClassLibrary1
{
public class Library: IEnumerable<string>
{
public IEnumerator<string> GetEnumerator()
{
yield return $"{Environment.CurrentDirectory}";
yield return $"{Directory.GetCurrentDirectory()}";
yield return $"{GetType().Assembly.Location}";
yield return $"{Process.GetCurrentProcess().MainModule.FileName}";
yield return $"{AppDomain.CurrentDomain.BaseDirectory}";
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
LinuxPathTest
using ClassLibrary1;
using System;
using System.Diagnostics;
using System.IO;
namespace LinuxPathTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("-------------------------------");
foreach (var item in new Library())
{
Console.WriteLine($"{item}");
}
Console.WriteLine("-------------------------------");
Console.WriteLine($"{Environment.CurrentDirectory}");
Console.WriteLine($"{Directory.GetCurrentDirectory()}");
Console.WriteLine($"{(new object()).GetType().Assembly.Location}");
Console.WriteLine($"{Process.GetCurrentProcess().MainModule.FileName}");
Console.WriteLine($"{AppDomain.CurrentDomain.BaseDirectory}");
Console.WriteLine($"{AppDomain.CurrentDomain.SetupInformation.ApplicationBase}"); // .net standard 不支持
Console.ReadLine();
}
}
}
输出
win-x64
可执行文件所在路径 E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish
-------------------------------
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish
C:\Users\用户名\AppData\Local\Temp\.net\LinuxPathTest\3c0ont0d.sts\ClassLibrary1.dll
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish\LinuxPathTest.exe
C:\Users\用户名\AppData\Local\Temp\.net\LinuxPathTest\3c0ont0d.sts\
-------------------------------
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish
C:\Users\用户名\AppData\Local\Temp\.net\LinuxPathTest\3c0ont0d.sts\System.Private.CoreLib.dll
E:\Projects\LinuxPathTest\LinuxPathTest\bin\Release\netcoreapp3.1\publish\LinuxPathTest.exe
C:\Users\用户名\AppData\Local\Temp\.net\LinuxPathTest\3c0ont0d.sts\
C:\Users\用户名\AppData\Local\Temp\.net\LinuxPathTest\3c0ont0d.sts\
linux-64
可执行文件路径 /home/用户名/Desktop/Test
-------------------------------
/home/用户名/Desktop/Test
/home/用户名/Desktop/Test
/var/tmp/.net/LinuxPathTest/5kkarywl.a5s/ClassLibrary1.dll
/home/用户名/Desktop/Test/LinuxPathTest
/var/tmp/.net/LinuxPathTest/5kkarywl.a5s/
-------------------------------
/home/用户名/Desktop/Test
/home/用户名/Desktop/Test
/var/tmp/.net/LinuxPathTest/5kkarywl.a5s/System.Private.CoreLib.dll
/home/用户名/Desktop/Test/LinuxPathTest
/var/tmp/.net/LinuxPathTest/5kkarywl.a5s/
/var/tmp/.net/LinuxPathTest/5kkarywl.a5s/
总结
.Net Core 打包为自宿主无依赖的单文件之后,因为多出一步将文件释放到临时文件的操作,
一些获取文件路径的方式得到的结果可能和.Net Framework 下不一致。