Can someone help to through this?

Hi all,

I’m having some trouble working with relative paths in libraries.

I created a library called “ReadJson” that takes an absolute or relative path as input and returns a JSON object. It works fine with absolute paths.

However, I’d like it to work with relative paths as well, and that’s where I’m running into issues. After reading through some forums and documentation, I realized that when a library is published and then installed in a project, using a relative path will refer to the library’s package location — not the location of the project that’s calling the library.

I’ve tried several methods to get the path of the calling project, because my goal is for the library to dynamically resolve paths relative to the main project (not the library location). Here are the approaches I tried:

System.Environment.CurrentDirectory
Directory.GetCurrentDirectory
Path.GetDirectoryName(Environment.CurrentDirectory)

Unfortunately, none of these gave me the result I expected.

Could someone please help me figure out how to properly handle relative paths in this case?

Thanks a lot!

@Ferran_GARCIA

I hope this helps

Cheers

you when you said “none of these gave me the result I expected”, tell us what is the issue that you are facing. Environment.CurrentDirectory should work just fine.

did you tried all the above things in library itself? if yes then pass only the absolute path to library instead of relative path

Hi @Ferran_GARCIA

try to pass the main project’s base folder path as an input argument to your library**, then inside the library combine it with the relative path using Path.Combine.

Like below
fullPath = Path.Combine(baseProjectPath, relativePath)

Happy Automation