0

I have two apps that share a data files folder. One is an asp.net core web app. The other is a .net core console app. Folder structure is:

  WebApp
  WorkflowApp
  Datafiles

If I use any of these in the console app:

  string en = Environment.CurrentDirectory;
  string dr = Directory.GetCurrentDirectory();
  string ad = AppDomain.CurrentDomain.BaseDirectory;
  string sa = System.AppContext.BaseDirectory;

they all point to: workflow\bin\Debug\netcoreapp2.0

If I use them in the webapp, then en & dr point to "webapp" and the others point to "webapp\bin\Debug\netcoreapp2.0

I'm using appsettings.json to set the path to Datafiles. This file is shared by WebApp and WorkflowApp. I was setting it to "../Datafiles" and then getting the full path using:

  Path.Combine(Directory.GetCurrentDirectory(), datafiles);

But this only works in WebApp. I would like to find a common method that works for both. And I don't want to set an absolute path in appsettings.json.

I could use AppDomain.CurrentDomain.BaseDirectory and then use:

  @"..\..\..\..\Datafiles"

to move back from a "bin\Debug\netcoreapp2.0" folder. But this seems a bit convoluted. Is there a better way?

EDIT: An answer to this question might solve my problem (at least when in development): How can I get the path to the project folder (where the .csproj file is located) from in the code?

John Pankowicz
  • 4,203
  • 2
  • 29
  • 47
  • From what I understand, you want to get path to solution directory. All the above commands are to get path to project directory. You can try this solution, but not sure if it's the correct way. Edit1 - link : https://stackoverflow.com/a/35824406/7819056 – Rishi Shah Jan 28 '20 at 02:52
  • 1
    @Rishi No, I want to get to the project directory in both cases. For the webapp, the first 2 methods get to the project directory. But for the console app, none of them do. – John Pankowicz Jan 28 '20 at 02:58
  • 1
    After some searching I found that this is default behavior. Look at this thread https://github.com/dotnet/cli/issues/4473. Specifically https://github.com/dotnet/cli/issues/4473#issuecomment-256531057. Not sure how to overcome it. Hope this helps :) – Rishi Shah Jan 28 '20 at 03:20
  • @RishiShah Thanks. That explains why I have this problem. CurrentDirectory used to be the project folder but was changed to support dotnet cli. – John Pankowicz Jan 28 '20 at 19:48

2 Answers2

1

Try this way.

 var requiredPath = "";
 var address = "DataFiles";
 var rootDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        if (rootDir != null)
        {
            requiredPath = Path.Combine(rootDir, address);
        }
Tony Tom
  • 1,435
  • 1
  • 10
  • 17
  • I get the same as when I use AppDomain.CurrentDomain.BaseDirectory. It points to ....bin\Debug\netcoreapp2.0. Like with that, I will need to go up 4 levels and to get to Datafiles. Maybe either of those is the only way to do it. – John Pankowicz Jan 28 '20 at 17:14
0

I would not let the application figure out the directory / path of where the data files are but rather externalize the paths into a config such as in appSettings.json or something similar.

If you then want to take it a step further and have the apps share the same config you can use a cetnralized store such as Consul or Etcd

https://github.com/etcd-io/etcd

https://www.consul.io/api/kv.html

  • I am already using appsettings.json to specify the location of the Datafiles folder. But I need to specify it in relation to something. I don't want to use absolute paths. I thought I could specify it in relation to CurrentDirectory. But the value of CurrentDirectory is different depending on whether the code is running in a console app or a web app. – John Pankowicz Jan 28 '20 at 14:16