-
Hi, I would like to read text input from a file in my Benchmark runs, but I'm having trouble with using relative paths. My code reads the files just fine outside of the Benchmark, but it can't find the files when the Benchmark runs. I get a DirectoryNotFoundException:
The way I currently have it set up is referring to the file I want to read relative to the Here is my benchmark class: https://github.com/lbussell/advent-of-code/blob/cc638c0648739df800c8045053f9f058e9fcc377/src/AdventOfCode.Cli/SolutionBenchmark.cs#L25 And here is where I read the file. It is read as part of the constructor for each class implementation that I want to run a benchmark on: https://github.com/lbussell/advent-of-code/blob/cc638c0648739df800c8045053f9f058e9fcc377/src/AdventOfCode.Cli/Solutions/SolutionWithTextInput.cs#L7 I would appreciate any advice on getting this to work. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You can use a local helper function like: static string WhereAmI([CallerFilePath] string callerFilePath = "") => callerFilePath; and calculate the path relative to the source code file that's calling the function, e.g. string basePath = Path.GetDirectoryName(WhereAmI());
string targetPath = Path.Combine(basePath, "relative/path/to/file.txt"); For a more complex approach, dealing with deterministic paths, CI, etc. see |
Beta Was this translation helpful? Give feedback.
You can use a local helper function like:
and calculate the path relative to the source code file that's calling the function, e.g.
For a more complex approach, dealing with deterministic paths, CI, etc. see
https://github.com/mawosoft/MissingCoverage/blob/master/tests/testdata/src/TestDataDirectory.cs