You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
.NET Core console app default working directory is the source directory, not the bin directory #589
Reacted by Aron Adler, roblapp, Joydeep Roychowdhury, Tawani Anyangwe, Miguel Hau, Luis Cantero, Alexander Veselov, Nurullo Sulaymonov and Evgeny Gorbovoy
@pi3k14 if ist a bug, a better way today to get real directory is by Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?
Reacted by Roberto Rabasco, Darran, Son Do, Luis Harvey Triana Vega, FrederickBrier, Elan Hasson, Bill, S. Mohassel, Felipe Martínez, Gerald H. and 30 more
Reacted by Son Do, Homan, Bill, Natia Gadelia, Matthew Thomas, Olamide Onakoya and Marcelo Vega Janhsen
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.
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 ). .
@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:
varbuilder=newHostBuilder()// .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()){varconfigFile="configuration/NotifyServices-App";config.AddJsonFile(configFile,optional:true);varconfigPath=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}.");}}})....
Activity
davkean commentedon Oct 11, 2016
Wasn't this needed for ASP.NET as they run from source?
srivatsn commentedon Oct 11, 2016
I see. Tagging @BillHiebert to confirm.
pi3k14 commentedon May 19, 2017
What? Regardless of what ASP.NET needs, this is a bug for Console apps
davkean commentedon 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 commentedon May 19, 2017
@davkean, will do
alexsandro-xpt commentedon Jun 29, 2017
@pi3k14 if ist a bug, a better way today to get real directory is by
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)?pi3k14 commentedon Jul 10, 2017
@alexsandro-xpt yes, as documented in issue #2239
nathanchere commentedon Dec 2, 2017
This is still an issue. The same code does two different things if you
dotnet runvsdotnet test, and needing to detect if you are running as a test or console application at runtime is pretty ugly.Pilchie commentedon Dec 4, 2017
@BillHiebert - can you take a look?
BillHiebert commentedon Dec 4, 2017
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 commentedon Dec 5, 2017
@Pilchie @BillHiebert It's an open bug, reported in #2239
BillHiebert commentedon Dec 6, 2017
@Pilchie, @davkean - I think we should fix this
Pilchie commentedon Dec 6, 2017
@BillHiebert agreed, but tracked in #2239, right?
BillHiebert commentedon Dec 6, 2017
@Pilchie - yes
Lobosque commentedon Jan 30, 2019
this is still a problem in the context that @nathanchere mentioned
alexsandro-xpt commentedon Jan 31, 2019
@srivatsn Are you test it if have same behavior in WebApp? I'm ask it because I test it with
Hosted Serviceand theConfigureAppConfigurationmethod I should set base pathconfig.SetBasePath(Directory.GetCurrentDirectory());to readappsettings.jsonfile:Update Configuration.cs