Working directory for ASP.NET Core WebApplication is set to project folder
See original GitHub issueVisual Studio Version: 16.1.6
Summary: When I run a newly created empty WebApplication and start Debugging the WorkingDirectory is the projects root folder. When I create an empty console application it is the Output Directory. I expect both to be the same (and am quite sure this was the case some time in the past?).
Steps to Reproduce:
-
Create an empty web application project with .Net Core 2.1 or 2.2.
-
Paste this code:
Console.WriteLine("CurrentDirectory in Main: {0}", System.IO.Directory.GetCurrentDirectory());
-
Run the project without IIS (IIS will show the same behavior but logging it is a bit more complicated).
Expected Behavior:
The output is CurrentDirectory in Main: {ProjectPath}\bin\Debug\netcoreapp2.2
Actual Behavior:
The output is CurrentDirectory in Main: {ProjectPath}
User Impact: I can’t use a library that expects the WorkingDirectory to be in the output directory without adding some code changing the working directory to where it should have been in the first place.
Other notes This issue seems to have been reappearing and fixed for console applications and now seems to appear for web applications as well.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:30
- Comments:16 (7 by maintainers)
For me the problem is that one of the libs (nuget package RandomPersonLib) is looking for its content files using relative path. In our solution we have 2 ways of execution, one is console and second is WebApp. From console everything is working fine but when we run WebApp exception is thrown because RandomPersonLib can’t find required data files. So I manually added folder with data to WebApi project folder, however it is not really nice solution. This required data folder is included in WebApi/bin/Debug (as it is set to Build Action: Content, Copy if newer) but while default directory is WebApi it can’t be found by RandomPersonLib package.
Sorry maybe my comment wasn’t clear enough. I was talking about changing the current directory simply with Directory.SetCurrentDirectory