question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Working directory for ASP.NET Core WebApplication is set to project folder

See original GitHub issue

Visual 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:

  1. Create an empty web application project with .Net Core 2.1 or 2.2.

  2. Paste this code:

    Console.WriteLine("CurrentDirectory in Main: {0}", System.IO.Directory.GetCurrentDirectory());
    
  3. 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.

See #2239 and #589

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:30
  • Comments:16 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
GondilOndrocommented, Apr 24, 2020

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.

2reactions
jmevelcommented, Jan 9, 2022

@jmevel Can you elaborate on how you accomplish this?

I tried setting the working directory in the project file

<PropertyGroup>
  <RunWorkingDirectory>$(TargetDir)</RunWorkingDirectory>
<PropertyGroup>

But TargetDir, TargetPath, etc. are returning empty values.

Sorry maybe my comment wasn’t clear enough. I was talking about changing the current directory simply with Directory.SetCurrentDirectory

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get root directory of project in asp.net core. ...
GetCurrentDirectory() returns the current directory, which may or may not be the folder where the application is located.
Read more >
Static files in ASP.NET Core
Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with ......
Read more >
How do I find which directory my .NET Core console ...
I have a CLI app in .NET Core 3 that is supposed to sit in your apps folder you have in your path,...
Read more >
Working with Static Files - ASP.NET Core Documentation
Static files are typically located in the web root (<content-root>/wwwroot) folder. See Content root and Web root in Introduction to ASP.NET Core for...
Read more >
wwwroot folder in ASP.NET Core
When you create a new ASP.NET Core Web Application with Web and MVC Template, then by default, this folder (wwwroot) is created in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found