user1409708
user1409708

Reputation: 1053

Visual Studio File Path Windows vs. Linux

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

Upvotes: 0

Views: 1199

Answers (2)

cly
cly

Reputation: 708

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.

Upvotes: 1

frankhommers
frankhommers

Reputation: 1305

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)

Upvotes: 1

Related Questions