M
M
Maxim K2020-05-18 12:44:08
C++ / C#
Maxim K, 2020-05-18 12:44:08

What is the correct way to change from an absolute path to a relative path in C#?

Hello.

I use an absolute path in all my code: How to change the path correctly so that the xml file is next to the exe? There are solutions on the Internet, but to do so - all for some reason controversial THANKS
string filename = @"c:\Users\1\source\test.xml";



Answer the question

In order to leave comments, you need to log in

4 answer(s)
#
#, 2020-05-18
@mkvmaks

What is the correct way to change the path so that the xml file is next to the exe?
first change the task in your head (what is the cause? and what is the effect?)
- in order for the xml file to be next to the exe , you need to put / create it next to the exe
- then you don’t need to
write out the path - if you created / edit the xml file in the project, then usually the path to the binary is something like this .\bin\Debug\in this case, the relative path to such a file will be something like ..\..\
ps do you think they find their directory, the directory for folders with user settings, directories of documents, pictures, etc.. other programs?
discover:
environment variables in general
and c# environment variables in particular
and then you will notice classes/methods likepath , path.combine and environment.specialfolder

D
d-stream, 2020-05-18
@d-stream

Dance from https://docs.microsoft.com/ru-ru/dotnet/api/system...

D
Developer, 2020-05-18
@samodum

Several options to choose from depending on the type of your project:
https://stackoverflow.com/questions/6041332/best-w...

V
Vladimir Korotenko, 2020-05-18
@firedragon

Write the path to the config and use it as the base one. There will be fewer surprises, especially if you are not allowed to write to the folder next to the binary.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question