C
C#8mo ago
Gipper

How to find relative file path for file inside of project?

Dealing with a C# console application, I have a folder inside the project named Files and inside that folder I have a file Servicos_A.csv. I want to get a relative path to that file for File I/O. My concern with using absolute path is that when I push it to github it won't work in my colleague's computer cause obviously his filesystem will be different from mine. How can I get to that path? I already searched it and asked generative AIs but it won't work, mostly because using the string assemblyDirectory = AppDomain.CurrentDomain.BaseDirectory utility for a part of the path seems to add some unnecessary and non-existing directories into the path for some reason.
1 Reply
SpReeD
SpReeD8mo ago
As long it has no build-action like Embedded Resource, and has an output on build time like CopyIfNewer it's inside the same root as your dll or exe, that means using Environment.CurrentDirectory let's you determine the root. From there you build your path with the Path class. Example:
string pathtoMyFile = Path.Combine(Environment.CurrentDirectory, "mySubFolder", "yetAnotherSub", "foobar.file");
string pathtoMyFile = Path.Combine(Environment.CurrentDirectory, "mySubFolder", "yetAnotherSub", "foobar.file");
Want results from more Discord servers?
Add your server