C
C#2y ago
jgomezal

❔ 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
Doombox
Doombox2y ago
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
jgomezal
jgomezalOP2y ago
@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";
Doombox
Doombox2y ago
is the file actually there?, sounds stupid but yeah
jgomezal
jgomezalOP2y ago
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/"
Doombox
Doombox2y ago
ah right, yeah I see now, sorry read too quickly
jgomezal
jgomezalOP2y ago
Look. It's the appsettings.json
jgomezal
jgomezalOP2y ago
When I open that fiory terminal application, this is the result.
Doombox
Doombox2y ago
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); instead
jgomezal
jgomezalOP2y ago
On 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.
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server