Skip to content

.NET Core console app default working directory is the source directory, not the bin directory #589

Closed

Description

@srivatsn
  1. Create a netcore console app and replace Main with this:
   Console.WriteLine(Directory.GetCurrentDirectory());
  1. Ctrl+F5

Expected something like this:

C:\ConsoleApplication1\ConsoleApplication1\bin\Debug

Got this:

C:\ConsoleApplication1\ConsoleApplication1

Activity

  1. added this to the milestone on Oct 11, 2016
  2. davkean commented on Oct 11, 2016

    @davkean
    Member

    Wasn't this needed for ASP.NET as they run from source?

  3. srivatsn commented on Oct 11, 2016

    @srivatsn
    ContributorAuthor

    I see. Tagging @BillHiebert to confirm.

  4. pi3k14 commented on May 19, 2017

    @pi3k14

    What? Regardless of what ASP.NET needs, this is a bug for Console apps

  5. davkean commented on May 19, 2017

    @davkean
    Member

    @pi3k14 If this is returning the project folder for you (I can't test right now), can you open another bug?

  6. pi3k14 commented on May 19, 2017

    @pi3k14

    @davkean, will do

  7. alexsandro-xpt commented on Jun 29, 2017

    @alexsandro-xpt

    @pi3k14 if ist a bug, a better way today to get real directory is by Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?

  8. pi3k14 commented on Jul 10, 2017

    @pi3k14

    @alexsandro-xpt yes, as documented in issue #2239

  9. nathanchere commented on Dec 2, 2017

    @nathanchere

    This is still an issue. The same code does two different things if you dotnet run vs dotnet test, and needing to detect if you are running as a test or console application at runtime is pretty ugly.

  10. Pilchie commented on Dec 4, 2017

    @Pilchie
    Member

    @BillHiebert - can you take a look?

  11. BillHiebert commented on Dec 4, 2017

    @BillHiebert
    Contributor

    Based on a conversation I had with @eerhardt, this was done for consistency. Here's an excerpt from that thread:
    _By default <RunWorkingDirectory> won’t be set by the SDK any more. So VS will have to default the working directory during F5 when RunWorkingDirectory is blank. For ASP.Net apps, the default has to be the project directory. For console apps, to be consistent with the CLI and ASP.NET, it probably should also be the project directory – just like it was for .xproj_
    I don't think it matters much for console applications, unless the console app happens to be running asp.net core application (which does happen. I've seem customers run their webapp directly using dotnet.exe and an Executable profile ). .

  12. pi3k14 commented on Dec 5, 2017

    @pi3k14

    @Pilchie @BillHiebert It's an open bug, reported in #2239

  13. BillHiebert commented on Dec 6, 2017

    @BillHiebert
    Contributor

    @Pilchie, @davkean - I think we should fix this

  14. Pilchie commented on Dec 6, 2017

    @Pilchie
    Member

    @BillHiebert agreed, but tracked in #2239, right?

  15. BillHiebert commented on Dec 6, 2017

    @BillHiebert
    Contributor

    @Pilchie - yes

  16. Lobosque commented on Jan 30, 2019

    @Lobosque

    this is still a problem in the context that @nathanchere mentioned

  17. alexsandro-xpt commented on Jan 31, 2019

    @alexsandro-xpt

    @srivatsn Are you test it if have same behavior in WebApp? I'm ask it because I test it with Hosted Service and the ConfigureAppConfiguration method I should set base path config.SetBasePath(Directory.GetCurrentDirectory()); to read appsettings.json file:

    var builder = new HostBuilder()
        // .UseContentRoot(Directory.GetCurrentDirectory())
        // .UseEnvironment(EnvironmentName.Development)
        .ConfigureHostConfiguration(configHost =>
        {
            configHost.AddEnvironmentVariables(prefix: "ASPNETCORE_");
            configHost.AddCommandLine(args);
        })
        .ConfigureAppConfiguration((context, config) =>
        {
            config.SetBasePath(Directory.GetCurrentDirectory()); // <-----------------------------------------
            // config.SetBasePath(AppContext.BaseDirectory);
            config.AddEnvironmentVariables(prefix: "ASPNETCORE_");
            config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
            config.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", optional: true);
            config.AddCommandLine(args);
    
            if (context.HostingEnvironment.IsProduction())
            {
                var configFile = "configuration/NotifyServices-App";
                config.AddJsonFile(configFile, optional: true);
                var configPath = Path.Combine(context.HostingEnvironment.ContentRootPath, configFile);
                if (!File.Exists(configPath))
                {
                    System.Console.WriteLine($"O arquivo ${configPath} não foi encontrado, ele é importante mas não necessário no ambiente {context.HostingEnvironment.EnvironmentName}.");
                }
            }
        })....
  18. added a commit that references this issue on Jun 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions