A
A
Anton Baryshev2021-05-22 14:29:35
.NET
Anton Baryshev, 2021-05-22 14:29:35

How to get the path to the file so that everything works both during development and deployment?

Hello. I am writing a small web application for educational purposes. I created a separate layer for working with data, which is a library. At the time of development, the folder structure is as follows:
60a8e8dcc8cde136451333.png
The following JSON file is stored in the configuration folder, its code will be deserialized into an object of the DatabaseConfiguration type, so that you can conveniently retrieve the necessary data later:
60a8e9f6683f4893058487.png

I encountered one problem. I need to get the path to this file regardless of the machine and whether the application is deployed or not. Environment.CurrentDirectory returns the path to the executable, Directory.GetCurrentDirectory() also returns the path to the executable. But what I need is, as it were, the main directory of the application, where the folders with the configuration, data contexts, models, and so on are located. Help. Tell me how to get it.
Thanks in advance.
PS If you have suggestions on how we could improve the configuration system in the data layer, then tell us.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
d-stream, 2021-05-22
@AVollane

Why not dance from the location of the executable file?
For example ..\..\config
ps Only Environment.CurrentDirectory - a bit shaky thing, better Assembly.GetExecutingAssembly().Location

V
Vladimir Korotenko, 2021-05-22
@firedragon

Use system.io.directory to get the directory path from the full path. And in general, use appconfig.json and sections in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question