-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Description
When calling Assembly.Get*Assembly().CodeBase for a self-contained .NET Core 3 project, it reports a temporary directory rather than the folder where the executable actually is located.
using System;
using System.Reflection;
namespace AssemblyReflectionIssue
{
class Program
{
static void Main(string[] args)
{
foreach (var file in Assembly.GetEntryAssembly().GetFiles())
{
Console.WriteLine(file.Name);
}
Console.WriteLine(Assembly.GetCallingAssembly().CodeBase);
Console.WriteLine(Assembly.GetEntryAssembly().CodeBase);
Console.WriteLine(Assembly.GetExecutingAssembly().CodeBase);
}
}
}
Put the code above in a project with these settings:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<LangVersion>8.0</LangVersion>
<Platforms>x64</Platforms>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
</Project>
Then publish the app to e.g. C:\AssemblyReflectionIssue and launch it. Something like this will be printed:
C:\Users\user\AppData\Local\Temp.net\AssemblyReflectionIssue\2bn5yvkd.pjb\AssemblyReflection.dll
file:///C:/Users/user/AppData/Local/Temp/.net/AssemblyReflectionIssue/2bn5yvkd.pjb/AssemblyReflection.dll
file:///C:/Users/user/AppData/Local/Temp/.net/AssemblyReflectionIssue/2bn5yvkd.pjb/AssemblyReflection.dll
file:///C:/Users/user/AppData/Local/Temp/.net/AssemblyReflectionIssue/2bn5yvkd.pjb/AssemblyReflection.dll
I would expect at least one of the calls to return the actual path (C:\AssemblyReflectionIssue)?