❔ Calling files Path inside the project (MAC and Windows"
Hello there.
It's a totally beginner question, for understanding how the compilation works. I have this folder structure:
project
|- Folder: Classes
|- Folder: Exports (here I'm downloading the API result)
|- Folder: MasterData (here there is a .csv file)
- File: project.cs
I know in MAC the files structure is "./MasterData/file.csv", and that Microsoft structure is ".\MasterData\file.csv".
Questions:
1. Once I compile the console application, will it recognize the file structure for whatever OS?
2. Must I include both path structures in the code with an if statement that confirms if the path exist?
Thanks a lot!
10 Replies
https://learn.microsoft.com/en-us/dotnet/api/system.io.path.directoryseparatorchar?view=net-7.0 when in doubt
I think
Path.Combine
/Path.Join
will also do that for you, if you're using path strings for stuff it's always better to be safe than sorry
but as far as I'm aware /
will work in Windows anyway when dealing with paths, not sure about Mac@Doombox linux and MAC use the same "/" separator. So, I guess this solution will help me. I will try it. Thanks a lot!
@Doombox
In the debugging and release option, the solution you shared worked fine.
However, I'm not sure what I'm doing wrong, because when I compiling the application, it doesn't matter the location of the folder, it's requesting for the files on my terminal homepage.
"Unhandled exception. System.IO.FileNotFoundException: Could not find file '/Users/juangomez/file.json"
Right now I have the application in the Downloads folder "/Users/juangomez/Downloads/Bodega/file.json";
The Path I used is: ".{separator}file.json";
is the file actually there?, sounds stupid but yeah
No issue, if any question, just ask, because all the information is needed.
Right now I have the application in this path:
"/Users/juangomez/Downloads/Bodega/"
I have the file.json in the same folder:
"/Users/juangomez/Downloads/Bodega/"
ah right, yeah I see now, sorry read too quickly
Look. It's the appsettings.json
When I open that fiory terminal application, this is the result.
yeah I guess the
.
directory specifier doesn't work like you're expecting on Mac, I don't know I never used Mac or even the .
character for paths, if the Json file is in the same folder as your executable you can use System.Reflection.Assembly.GetEntryAssembly().Location;
at least on windows to get the absolute path to the directory with your exe in
then Path.Combine(dir, file.json);
insteadOn it. Let me try.
@Doombox I need to double-check with someone who use MAC for developing this. I cloned the repository and using a Windows machine to compile it and it worked without issues. So, thank you so much for all your help.
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.