1) Create a netcore console app and replace Main with this:
Console.WriteLine(Directory.GetCurrentDirectory());
2) Ctrl+F5
Expected something like this:
C:\ConsoleApplication1\ConsoleApplication1\bin\Debug
Got this:
C:\ConsoleApplication1\ConsoleApplication1
Wasn't this needed for ASP.NET as they run from source?
I see. Tagging @BillHiebert to confirm.
What? Regardless of what ASP.NET needs, this is a bug for Console apps
@pi3k14 If this is returning the project folder for you (I can't test right now), can you open another bug?
@davkean, will do
@pi3k14 if ist a bug, a better way today to get real directory is by Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) ?
@alexsandro-xpt yes, as documented in issue #2239
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.
@BillHiebert - can you take a look?
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 ). .
@Pilchie @BillHiebert It's an open bug, reported in https://github.com/dotnet/project-system/issues/2239
@Pilchie, @davkean - I think we should fix this
@BillHiebert agreed, but tracked in #2239, right?
@Pilchie - yes
this is still a problem in the context that @nathanchere mentioned
@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}.");
}
}
})....
Most helpful comment
@pi3k14 if ist a bug, a better way today to get real directory is by
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)?