Skip to content

Not possible to get the executable path for self-contained executable #13051

@sirjeppe

Description

@sirjeppe

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)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-AssemblyLoader-coreclrquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions