Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
srivatsn opened this issue Oct 11, 2016 · 16 comments
Assignees
Labels
Bug This is a functional issue in already written code.

Comments

@srivatsn
Copy link
Contributor

  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

@srivatsn srivatsn added the Bug This is a functional issue in already written code. label Oct 11, 2016
@srivatsn srivatsn added this to the 1.0 RC milestone Oct 11, 2016
@davkean
Copy link
Member

davkean commented Oct 11, 2016

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

@srivatsn
Copy link
Contributor Author

I see. Tagging @BillHiebert to confirm.

@pi3k14
Copy link

pi3k14 commented May 19, 2017

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

@davkean
Copy link
Member

davkean commented May 19, 2017

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

@pi3k14
Copy link

pi3k14 commented May 19, 2017

@davkean, will do

@alexsandro-xpt
Copy link

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

@pi3k14
Copy link

pi3k14 commented Jul 10, 2017

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

@nathanchere
Copy link

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.

@Pilchie
Copy link
Member

Pilchie commented Dec 4, 2017

@BillHiebert - can you take a look?

@BillHiebert
Copy link
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 ). .

@pi3k14
Copy link

pi3k14 commented Dec 5, 2017

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

@BillHiebert
Copy link
Contributor

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

@Pilchie
Copy link
Member

Pilchie commented Dec 6, 2017

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

@BillHiebert
Copy link
Contributor

@Pilchie - yes

@Lobosque
Copy link

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

@alexsandro-xpt
Copy link

@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}.");
            }
        }
    })....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This is a functional issue in already written code.
Projects
None yet
Development

No branches or pull requests

9 participants