C
C#4mo ago
Jasonnn

Relative paths with assemblies calling each other

I have parent_dir/HaploDeep/models/some_files.onnx, parent_dir/HaploDeep/HaploDeep.cs and parent_dir/HaploDeep.Tests/Tests.cs In HaploDeep.cs I want to open models/files.onnx so I do
private readonly string[] _modelPaths =
[
@"models\model_1.onnx",
@"models\model_2.onnx",
@"models\model_3.onnx",
@"models\model_4.onnx",
@"models\model_5.onnx"
];
// Load them by using the array _modelPaths
private readonly string[] _modelPaths =
[
@"models\model_1.onnx",
@"models\model_2.onnx",
@"models\model_3.onnx",
@"models\model_4.onnx",
@"models\model_5.onnx"
];
// Load them by using the array _modelPaths
but when I call the unit tests it says System.IO.IOException: Model file models\model_1.onnx does not exists. I tried to do that:
string basePath = AppDomain.CurrentDomain.BaseDirectory;
_modelPaths = [
Path.Combine(basePath, @"models\model_1.onnx"),
Path.Combine(basePath, @"models\model_2.onnx"),
Path.Combine(basePath, @"models\model_3.onnx"),
Path.Combine(basePath, @"models\model_4.onnx"),
Path.Combine(basePath, @"models\model_5.onnx")
];
// Load them by using the array _modelPaths
string basePath = AppDomain.CurrentDomain.BaseDirectory;
_modelPaths = [
Path.Combine(basePath, @"models\model_1.onnx"),
Path.Combine(basePath, @"models\model_2.onnx"),
Path.Combine(basePath, @"models\model_3.onnx"),
Path.Combine(basePath, @"models\model_4.onnx"),
Path.Combine(basePath, @"models\model_5.onnx")
];
// Load them by using the array _modelPaths

in HaploDeep.cs But then when running my unit tests I have the same, it thinks the relative paths are relative to HaploDeep.Tests path: System.IO.IOException: Model file C:\...\HaploDeep.Tests\bin\Debug\net8.0\models\model_1.onnx does not exi... I've been using Python for years and if a path was relative in library 1, when library 2 would use library 1, the paths would be relative to library 1; not to library 2. And here I'm really confused. Would really appreciate some help, I've been banging my head on this for the past hour
8 Replies
Omnissiah
Omnissiah4mo ago
either you move the models in a third directory so that's independent from the projects themselves or you estabilish that Tests project depends on HaploDeep and search the models in there (there are other possiblities but i would keep it simple)
Jasonnn
Jasonnn4mo ago
By modifying the .csproj? I'm trying to use that:
<None Include="..\HaploDeep\models\*.onnx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Link>models\%(Filename)%(Extension)</Link>
</None>
<None Include="..\HaploDeep\models\*.onnx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
<Link>models\%(Filename)%(Extension)</Link>
</None>
It looks like it works but I'd like to make sure that sounds ok and not like a dirty trick to experienced people?
Omnissiah
Omnissiah4mo ago
i wouldn't do that i intended it in a more of a "logical" way in the sense that i meant you could hard-write that the path to the models must go outside HaploDeep.Tests and then inside HaploDeep, so they always must be together, because in the end that how it is at the moment
Jasonnn
Jasonnn4mo ago
Ah I see But the day I want to distribute the software (Imagine we're not talking about unit tests but a proper software that needs to use my other library HaploDeep) I would be forced to do that, otherwise the "models" folder doesn't end up in the bin which is the thing to distribute, right? I'm fairly new to C# sorry if I'm jsut spitting out nonsense
Omnissiah
Omnissiah4mo ago
i wouldn't say this is about just c#, in general it's about a strategy of distribution if you hard-code a path then you can say to the potential user "put the stuff there" if the project has this path in a configuration file then the user can choose to put a relative path or an absolute one bin is the compiled stuff, but when you distribute that you would still have to build a folder where everything sits right especially in cases where there are resources like this
Jasonnn
Jasonnn4mo ago
ah yeah i guess a configuration file makes sense thanks!! why did you think that using Include in the .csproj wouldn't be good practice?
Omnissiah
Omnissiah4mo ago
mainly because it seems like a problem that should be solved before that point if artifacts like these gets copied around then it can happen that the project folder gets messy and stuff works in unexpected ways because you don't remember there's this step in the building process or other not funny things
Jasonnn
Jasonnn4mo ago
I see, thanks
Want results from more Discord servers?
Add your server