0

I have some confusion about Windows path (dev machine) and Linux path (target OS for microservice)

I created a new folder in my c# project (app/yamls), put inside a file aaa.yaml and put the file in git ignore. On my windows dev machine, I see the file in c:\myproject\app\yamls. However, my microservice will run on Linux and aaa.yaml file will reside in /app/yamls.

When I run the following, I get file is missing

string file = "/app/yamls/aaa.yaml";
System.IO.File.Exists(file) // FALSE

What should be done so the new added file in visual studio will be seen as file residing in /app/yamls like it will be in production.

Thanks

user1409708
  • 953
  • 2
  • 11
  • 20
  • Check this answer. It may help u. https://stackoverflow.com/questions/18266637/why-does-system-io-file-existsstring-path-return-false – D A Nov 16 '21 at 09:27
  • How you deploy your project from a Windows machine to a Linux machine is up to you. Technically you could [setup a file share](https://www.howtogeek.com/176471/how-to-share-files-between-windows-and-linux/) so a post-build event copies the file where you want it, but that's probably not worth the hassle. – Hans Passant Nov 16 '21 at 10:17

2 Answers2

1

You need to code file and directory handling in a cross platform manner. You could something like this: https://learn.microsoft.com/en-us/dotnet/api/System.IO.Path.PathSeparator?view=net-5.0&viewFallbackFrom=netcore-5.0

That System.IO.Path.PathSeparator will be / on Linux and \ on Windows

What I usually do is create an appsettings.json file with file location in there. And then change the config file depending on where it's deployed (Linux or Windows)

frankhommers
  • 1,169
  • 12
  • 26
1

Path class has static members useful if manipulation of file paths required in .Net environment. Look at these:

Path.DirectorySeparatorChar
Path.AltDirectorySeparatorChar
Path.PathSeparator
Path.VolumeSeparatorChar

If you just need to create path from given components the Path.Combine(...) method is also useful.

cly
  • 661
  • 3
  • 13