Slip
Slip

Reputation: 603

How to detect OS my Asp.Net Core app is running on?

I have an Asp.Net Core 1.0.0 app that runs on both Linux and Windows. In the app I need to upload files and save them in wwwroot subfolders. I get IHostingEnvironment.WebRootPath and specify a subfolder. It works fine on Windows but not on Linux because of paths. Window's \ of course is not going to work. So how can I detect whether my app is running on Windows or Linux from controller and choose an appropriate path delimiter?

Upvotes: 1

Views: 1207

Answers (2)

HH321
HH321

Reputation: 552

You can use / on both Windows and Linux

Upvotes: 0

DavidG
DavidG

Reputation: 118947

Rather than this, you should use Path.Combine to generate directory names. For example:

var root = env.WebRootPath;
var myDirectory = Path.Combine(root, "subdirectory");

Upvotes: 3

Related Questions