Home > Blockchain >  Visual Studio File Path Windows vs. Linux
Visual Studio File Path Windows vs. Linux

Time:11-16

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

CodePudding user response:

You need to code file and directory handling in a cross platform manner. You could something like this: https://docs.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)

CodePudding user response:

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.

  • Related